Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75f4922

Browse files
committed
Static assertions cleanup
Because we added StaticAssertStmt() first before StaticAssertDecl(), some uses as well as the instructions in c.h are now a bit backwards from the "native" way static assertions are meant to be used in C. This updates the guidance and moves some static assertions to better places. Specifically, since the addition of StaticAssertDecl(), we can put static assertions at the file level. This moves a number of static assertions out of function bodies, where they might have been stuck out of necessity, to perhaps better places at the file level or in header files. Also, when the static assertion appears in a position where a declaration is allowed, then using StaticAssertDecl() is more native than StaticAssertStmt(). Reviewed-by: John Naylor <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/941a04e7-dd6f-c0e4-8cdf-a33b3338cbda%40enterprisedb.com
1 parent 2613dec commit 75f4922

File tree

29 files changed

+99
-104
lines changed

29 files changed

+99
-104
lines changed

src/backend/access/heap/heapam.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5783,10 +5783,6 @@ heap_finish_speculative(Relation relation, ItemPointer tid)
57835783

57845784
htup = (HeapTupleHeader) PageGetItem(page, lp);
57855785

5786-
/* SpecTokenOffsetNumber should be distinguishable from any real offset */
5787-
StaticAssertStmt(MaxOffsetNumber < SpecTokenOffsetNumber,
5788-
"invalid speculative token constant");
5789-
57905786
/* NO EREPORT(ERROR) from here till changes are logged */
57915787
START_CRIT_SECTION();
57925788

@@ -7921,7 +7917,7 @@ index_delete_sort(TM_IndexDeleteOp *delstate)
79217917
const int gaps[9] = {1968, 861, 336, 112, 48, 21, 7, 3, 1};
79227918

79237919
/* Think carefully before changing anything here -- keep swaps cheap */
7924-
StaticAssertStmt(sizeof(TM_IndexDelete) <= 8,
7920+
StaticAssertDecl(sizeof(TM_IndexDelete) <= 8,
79257921
"element size exceeds 8 bytes");
79267922

79277923
for (int g = 0; g < lengthof(gaps); g++)

src/backend/access/nbtree/nbtutils.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,13 +2486,6 @@ _bt_check_natts(Relation rel, bool heapkeyspace, Page page, OffsetNumber offnum)
24862486
Assert(offnum >= FirstOffsetNumber &&
24872487
offnum <= PageGetMaxOffsetNumber(page));
24882488

2489-
/*
2490-
* Mask allocated for number of keys in index tuple must be able to fit
2491-
* maximum possible number of index attributes
2492-
*/
2493-
StaticAssertStmt(BT_OFFSET_MASK >= INDEX_MAX_KEYS,
2494-
"BT_OFFSET_MASK can't fit INDEX_MAX_KEYS");
2495-
24962489
itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
24972490
tupnatts = BTreeTupleGetNAtts(itup, rel);
24982491

src/backend/access/transam/clog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
275275
bool all_xact_same_page)
276276
{
277277
/* Can't use group update when PGPROC overflows. */
278-
StaticAssertStmt(THRESHOLD_SUBTRANS_CLOG_OPT <= PGPROC_MAX_CACHED_SUBXIDS,
278+
StaticAssertDecl(THRESHOLD_SUBTRANS_CLOG_OPT <= PGPROC_MAX_CACHED_SUBXIDS,
279279
"group clog threshold less than PGPROC cached subxids");
280280

281281
/*

src/backend/access/transam/xlog.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3883,15 +3883,6 @@ WriteControlFile(void)
38833883
int fd;
38843884
char buffer[PG_CONTROL_FILE_SIZE]; /* need not be aligned */
38853885

3886-
/*
3887-
* Ensure that the size of the pg_control data structure is sane. See the
3888-
* comments for these symbols in pg_control.h.
3889-
*/
3890-
StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_MAX_SAFE_SIZE,
3891-
"pg_control is too large for atomic disk writes");
3892-
StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_FILE_SIZE,
3893-
"sizeof(ControlFileData) exceeds PG_CONTROL_FILE_SIZE");
3894-
38953886
/*
38963887
* Initialize version and compatibility-check fields
38973888
*/

src/backend/backup/basebackup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink)
370370
else
371371
{
372372
/* Properly terminate the tarfile. */
373-
StaticAssertStmt(2 * TAR_BLOCK_SIZE <= BLCKSZ,
373+
StaticAssertDecl(2 * TAR_BLOCK_SIZE <= BLCKSZ,
374374
"BLCKSZ too small for 2 tar blocks");
375375
memset(sink->bbs_buffer, 0, 2 * TAR_BLOCK_SIZE);
376376
bbsink_archive_contents(sink, 2 * TAR_BLOCK_SIZE);
@@ -1745,7 +1745,7 @@ _tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget,
17451745
* large enough to fit an entire tar block. We double-check by means
17461746
* of these assertions.
17471747
*/
1748-
StaticAssertStmt(TAR_BLOCK_SIZE <= BLCKSZ,
1748+
StaticAssertDecl(TAR_BLOCK_SIZE <= BLCKSZ,
17491749
"BLCKSZ too small for tar block");
17501750
Assert(sink->bbs_buffer_length >= TAR_BLOCK_SIZE);
17511751

src/backend/catalog/dependency.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ static const Oid object_classes[] = {
191191
TransformRelationId /* OCLASS_TRANSFORM */
192192
};
193193

194+
/*
195+
* Make sure object_classes is kept up to date with the ObjectClass enum.
196+
*/
197+
StaticAssertDecl(lengthof(object_classes) == LAST_OCLASS + 1,
198+
"object_classes[] must cover all ObjectClasses");
199+
194200

195201
static void findDependentObjects(const ObjectAddress *object,
196202
int objflags,
@@ -2550,12 +2556,6 @@ add_object_address(ObjectClass oclass, Oid objectId, int32 subId,
25502556
{
25512557
ObjectAddress *item;
25522558

2553-
/*
2554-
* Make sure object_classes is kept up to date with the ObjectClass enum.
2555-
*/
2556-
StaticAssertStmt(lengthof(object_classes) == LAST_OCLASS + 1,
2557-
"object_classes[] must cover all ObjectClasses");
2558-
25592559
/* enlarge array if needed */
25602560
if (addrs->numrefs >= addrs->maxrefs)
25612561
{

src/backend/executor/execExprInterp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
496496
&&CASE_EEOP_LAST
497497
};
498498

499-
StaticAssertStmt(lengthof(dispatch_table) == EEOP_LAST + 1,
499+
StaticAssertDecl(lengthof(dispatch_table) == EEOP_LAST + 1,
500500
"dispatch_table out of whack with ExprEvalOp");
501501

502502
if (unlikely(state == NULL))

src/backend/libpq/auth-scram.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ scram_mock_salt(const char *username)
14431443
* not larger than the SHA256 digest length. If the salt is smaller, the
14441444
* caller will just ignore the extra data.)
14451445
*/
1446-
StaticAssertStmt(PG_SHA256_DIGEST_LENGTH >= SCRAM_DEFAULT_SALT_LEN,
1446+
StaticAssertDecl(PG_SHA256_DIGEST_LENGTH >= SCRAM_DEFAULT_SALT_LEN,
14471447
"salt length greater than SHA256 digest length");
14481448

14491449
ctx = pg_cryptohash_create(PG_SHA256);

src/backend/libpq/hba.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ static const char *const UserAuthName[] =
127127
"peer"
128128
};
129129

130+
/*
131+
* Make sure UserAuthName[] tracks additions to the UserAuth enum
132+
*/
133+
StaticAssertDecl(lengthof(UserAuthName) == USER_AUTH_LAST + 1,
134+
"UserAuthName[] must match the UserAuth enum");
135+
130136

131137
static List *tokenize_expand_file(List *tokens, const char *outer_filename,
132138
const char *inc_filename, int elevel,
@@ -3117,11 +3123,5 @@ hba_getauthmethod(hbaPort *port)
31173123
const char *
31183124
hba_authname(UserAuth auth_method)
31193125
{
3120-
/*
3121-
* Make sure UserAuthName[] tracks additions to the UserAuth enum
3122-
*/
3123-
StaticAssertStmt(lengthof(UserAuthName) == USER_AUTH_LAST + 1,
3124-
"UserAuthName[] must match the UserAuth enum");
3125-
31263126
return UserAuthName[auth_method];
31273127
}

src/backend/port/atomics.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pg_extern_compiler_barrier(void)
5454
void
5555
pg_atomic_init_flag_impl(volatile pg_atomic_flag *ptr)
5656
{
57-
StaticAssertStmt(sizeof(ptr->sema) >= sizeof(slock_t),
57+
StaticAssertDecl(sizeof(ptr->sema) >= sizeof(slock_t),
5858
"size mismatch of atomic_flag vs slock_t");
5959

6060
#ifndef HAVE_SPINLOCKS
@@ -105,7 +105,7 @@ pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr)
105105
void
106106
pg_atomic_init_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val_)
107107
{
108-
StaticAssertStmt(sizeof(ptr->sema) >= sizeof(slock_t),
108+
StaticAssertDecl(sizeof(ptr->sema) >= sizeof(slock_t),
109109
"size mismatch of atomic_uint32 vs slock_t");
110110

111111
/*
@@ -181,7 +181,7 @@ pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_)
181181
void
182182
pg_atomic_init_u64_impl(volatile pg_atomic_uint64 *ptr, uint64 val_)
183183
{
184-
StaticAssertStmt(sizeof(ptr->sema) >= sizeof(slock_t),
184+
StaticAssertDecl(sizeof(ptr->sema) >= sizeof(slock_t),
185185
"size mismatch of atomic_uint64 vs slock_t");
186186

187187
/*

0 commit comments

Comments
 (0)