From c7c7e648466e1c70dd303570031e8b23bfbb38dc Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 8 Dec 2022 14:34:07 +0100 Subject: [PATCH 4/4] Use StaticAssertDecl where possible When the static assertion appears in a position where a declaration is allowed, then using StaticAssertDecl() is more native than StaticAssertStmt(). --- src/backend/access/heap/heapam.c | 2 +- src/backend/access/transam/clog.c | 2 +- src/backend/backup/basebackup.c | 4 ++-- src/backend/executor/execExprInterp.c | 2 +- src/backend/libpq/auth-scram.c | 2 +- src/backend/port/atomics.c | 6 +++--- src/backend/utils/adt/numeric.c | 3 ++- src/backend/utils/mmgr/aset.c | 6 +++--- src/backend/utils/mmgr/generation.c | 2 +- src/backend/utils/mmgr/slab.c | 2 +- src/common/checksum_helper.c | 10 +++++----- src/include/common/int128.h | 2 +- 12 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 383e1ddde4..42756a9e6d 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -7917,7 +7917,7 @@ index_delete_sort(TM_IndexDeleteOp *delstate) const int gaps[9] = {1968, 861, 336, 112, 48, 21, 7, 3, 1}; /* Think carefully before changing anything here -- keep swaps cheap */ - StaticAssertStmt(sizeof(TM_IndexDelete) <= 8, + StaticAssertDecl(sizeof(TM_IndexDelete) <= 8, "element size exceeds 8 bytes"); for (int g = 0; g < lengthof(gaps); g++) diff --git a/src/backend/access/transam/clog.c b/src/backend/access/transam/clog.c index 77d9894dab..8832efc7c1 100644 --- a/src/backend/access/transam/clog.c +++ b/src/backend/access/transam/clog.c @@ -275,7 +275,7 @@ TransactionIdSetPageStatus(TransactionId xid, int nsubxids, bool all_xact_same_page) { /* Can't use group update when PGPROC overflows. */ - StaticAssertStmt(THRESHOLD_SUBTRANS_CLOG_OPT <= PGPROC_MAX_CACHED_SUBXIDS, + StaticAssertDecl(THRESHOLD_SUBTRANS_CLOG_OPT <= PGPROC_MAX_CACHED_SUBXIDS, "group clog threshold less than PGPROC cached subxids"); /* diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c index 74fb529380..f28bf10df3 100644 --- a/src/backend/backup/basebackup.c +++ b/src/backend/backup/basebackup.c @@ -370,7 +370,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink) else { /* Properly terminate the tarfile. */ - StaticAssertStmt(2 * TAR_BLOCK_SIZE <= BLCKSZ, + StaticAssertDecl(2 * TAR_BLOCK_SIZE <= BLCKSZ, "BLCKSZ too small for 2 tar blocks"); memset(sink->bbs_buffer, 0, 2 * TAR_BLOCK_SIZE); bbsink_archive_contents(sink, 2 * TAR_BLOCK_SIZE); @@ -1744,7 +1744,7 @@ _tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget, * large enough to fit an entire tar block. We double-check by means * of these assertions. */ - StaticAssertStmt(TAR_BLOCK_SIZE <= BLCKSZ, + StaticAssertDecl(TAR_BLOCK_SIZE <= BLCKSZ, "BLCKSZ too small for tar block"); Assert(sink->bbs_buffer_length >= TAR_BLOCK_SIZE); diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c index 1dab2787b7..ec26ae506f 100644 --- a/src/backend/executor/execExprInterp.c +++ b/src/backend/executor/execExprInterp.c @@ -496,7 +496,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull) &&CASE_EEOP_LAST }; - StaticAssertStmt(EEOP_LAST + 1 == lengthof(dispatch_table), + StaticAssertDecl(EEOP_LAST + 1 == lengthof(dispatch_table), "dispatch_table out of whack with ExprEvalOp"); if (unlikely(state == NULL)) diff --git a/src/backend/libpq/auth-scram.c b/src/backend/libpq/auth-scram.c index ee7f52218a..c9bab85e82 100644 --- a/src/backend/libpq/auth-scram.c +++ b/src/backend/libpq/auth-scram.c @@ -1443,7 +1443,7 @@ scram_mock_salt(const char *username) * not larger than the SHA256 digest length. If the salt is smaller, the * caller will just ignore the extra data.) */ - StaticAssertStmt(PG_SHA256_DIGEST_LENGTH >= SCRAM_DEFAULT_SALT_LEN, + StaticAssertDecl(PG_SHA256_DIGEST_LENGTH >= SCRAM_DEFAULT_SALT_LEN, "salt length greater than SHA256 digest length"); ctx = pg_cryptohash_create(PG_SHA256); diff --git a/src/backend/port/atomics.c b/src/backend/port/atomics.c index ba274bed93..ff4771ae37 100644 --- a/src/backend/port/atomics.c +++ b/src/backend/port/atomics.c @@ -54,7 +54,7 @@ pg_extern_compiler_barrier(void) void pg_atomic_init_flag_impl(volatile pg_atomic_flag *ptr) { - StaticAssertStmt(sizeof(ptr->sema) >= sizeof(slock_t), + StaticAssertDecl(sizeof(ptr->sema) >= sizeof(slock_t), "size mismatch of atomic_flag vs slock_t"); #ifndef HAVE_SPINLOCKS @@ -105,7 +105,7 @@ pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr) void pg_atomic_init_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val_) { - StaticAssertStmt(sizeof(ptr->sema) >= sizeof(slock_t), + StaticAssertDecl(sizeof(ptr->sema) >= sizeof(slock_t), "size mismatch of atomic_uint32 vs slock_t"); /* @@ -181,7 +181,7 @@ pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_) void pg_atomic_init_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 val_) { - StaticAssertStmt(sizeof(ptr->sema) >= sizeof(slock_t), + StaticAssertDecl(sizeof(ptr->sema) >= sizeof(slock_t), "size mismatch of atomic_uint64 vs slock_t"); /* diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index 7f0e93aa80..813e9d777d 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -4176,7 +4176,8 @@ int64_div_fast_to_numeric(int64 val1, int log10val2) { static int pow10[] = {1, 10, 100, 1000}; - StaticAssertStmt(lengthof(pow10) == DEC_DIGITS, "mismatch with DEC_DIGITS"); + StaticAssertDecl(lengthof(pow10) == DEC_DIGITS, "mismatch with DEC_DIGITS"); + if (unlikely(pg_mul_s64_overflow(val1, pow10[DEC_DIGITS - m], &val1))) { /* diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c index b6a8bbcd59..b45a34abce 100644 --- a/src/backend/utils/mmgr/aset.c +++ b/src/backend/utils/mmgr/aset.c @@ -306,7 +306,7 @@ AllocSetFreeIndex(Size size) tsize; /* Statically assert that we only have a 16-bit input value. */ - StaticAssertStmt(ALLOC_CHUNK_LIMIT < (1 << 16), + StaticAssertDecl(ALLOC_CHUNK_LIMIT < (1 << 16), "ALLOC_CHUNK_LIMIT must be less than 64kB"); tsize = size - 1; @@ -358,10 +358,10 @@ AllocSetContextCreateInternal(MemoryContext parent, AllocBlock block; /* ensure MemoryChunk's size is properly maxaligned */ - StaticAssertStmt(ALLOC_CHUNKHDRSZ == MAXALIGN(ALLOC_CHUNKHDRSZ), + StaticAssertDecl(ALLOC_CHUNKHDRSZ == MAXALIGN(ALLOC_CHUNKHDRSZ), "sizeof(MemoryChunk) is not maxaligned"); /* check we have enough space to store the freelist link */ - StaticAssertStmt(sizeof(AllocFreeListLink) <= (1 << ALLOC_MINBITS), + StaticAssertDecl(sizeof(AllocFreeListLink) <= (1 << ALLOC_MINBITS), "sizeof(AllocFreeListLink) larger than minimum allocation size"); /* diff --git a/src/backend/utils/mmgr/generation.c b/src/backend/utils/mmgr/generation.c index b432a92be3..1f17905b5a 100644 --- a/src/backend/utils/mmgr/generation.c +++ b/src/backend/utils/mmgr/generation.c @@ -167,7 +167,7 @@ GenerationContextCreate(MemoryContext parent, GenerationBlock *block; /* ensure MemoryChunk's size is properly maxaligned */ - StaticAssertStmt(Generation_CHUNKHDRSZ == MAXALIGN(Generation_CHUNKHDRSZ), + StaticAssertDecl(Generation_CHUNKHDRSZ == MAXALIGN(Generation_CHUNKHDRSZ), "sizeof(MemoryChunk) is not maxaligned"); /* diff --git a/src/backend/utils/mmgr/slab.c b/src/backend/utils/mmgr/slab.c index 6df0839b6a..c2f9bb6ad3 100644 --- a/src/backend/utils/mmgr/slab.c +++ b/src/backend/utils/mmgr/slab.c @@ -151,7 +151,7 @@ SlabContextCreate(MemoryContext parent, int i; /* ensure MemoryChunk's size is properly maxaligned */ - StaticAssertStmt(Slab_CHUNKHDRSZ == MAXALIGN(Slab_CHUNKHDRSZ), + StaticAssertDecl(Slab_CHUNKHDRSZ == MAXALIGN(Slab_CHUNKHDRSZ), "sizeof(MemoryChunk) is not maxaligned"); Assert(MAXALIGN(chunkSize) <= MEMORYCHUNK_MAX_VALUE); diff --git a/src/common/checksum_helper.c b/src/common/checksum_helper.c index 74d0cf0a2e..8948df4da1 100644 --- a/src/common/checksum_helper.c +++ b/src/common/checksum_helper.c @@ -177,15 +177,15 @@ pg_checksum_final(pg_checksum_context *context, uint8 *output) { int retval = 0; - StaticAssertStmt(sizeof(pg_crc32c) <= PG_CHECKSUM_MAX_LENGTH, + StaticAssertDecl(sizeof(pg_crc32c) <= PG_CHECKSUM_MAX_LENGTH, "CRC-32C digest too big for PG_CHECKSUM_MAX_LENGTH"); - StaticAssertStmt(PG_SHA224_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH, + StaticAssertDecl(PG_SHA224_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH, "SHA224 digest too big for PG_CHECKSUM_MAX_LENGTH"); - StaticAssertStmt(PG_SHA256_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH, + StaticAssertDecl(PG_SHA256_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH, "SHA256 digest too big for PG_CHECKSUM_MAX_LENGTH"); - StaticAssertStmt(PG_SHA384_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH, + StaticAssertDecl(PG_SHA384_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH, "SHA384 digest too big for PG_CHECKSUM_MAX_LENGTH"); - StaticAssertStmt(PG_SHA512_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH, + StaticAssertDecl(PG_SHA512_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH, "SHA512 digest too big for PG_CHECKSUM_MAX_LENGTH"); switch (context->type) diff --git a/src/include/common/int128.h b/src/include/common/int128.h index 8f035cf4cb..4b17aa9689 100644 --- a/src/include/common/int128.h +++ b/src/include/common/int128.h @@ -177,7 +177,7 @@ static inline void int128_add_int64_mul_int64(INT128 *i128, int64 x, int64 y) { /* INT64_AU32 must use arithmetic right shift */ - StaticAssertStmt(((int64) -1 >> 1) == (int64) -1, + StaticAssertDecl(((int64) -1 >> 1) == (int64) -1, "arithmetic right shift is needed"); /*---------- -- 2.38.1