Skip to content

Commit a7e5237

Browse files
committed
Fix asserts in fast-path locking code
Commit c4d5cb7 introduced a couple asserts in the fast-path locking code, upsetting Coverity. The assert in InitProcGlobal() is clearly wrong, as it assigns instead of checking the value. This is harmless, but doesn't check anything. The asserts in FAST_PATH_ macros are written as if for signed values, but the macros are only called for unsigned ones. That makes the check for (val >= 0) useless. Checks written as ((uint32) x < max) work for both signed and unsigned values. Negative values should wrap to values greater than INT32_MAX. Per Coverity, report by Tom Lane. Reported-by: Tom Lane Discussion: https://postgr.es/m/2891628.1727019959@sss.pgh.pa.us
1 parent 40708ac commit a7e5237

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/backend/storage/lmgr/lock.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,19 @@ int FastPathLockGroupsPerBackend = 0;
218218
* of fast-path lock slots.
219219
*/
220220
#define FAST_PATH_SLOT(group, index) \
221-
(AssertMacro(((group) >= 0) && ((group) < FastPathLockGroupsPerBackend)), \
222-
AssertMacro(((index) >= 0) && ((index) < FP_LOCK_SLOTS_PER_GROUP)), \
221+
(AssertMacro((uint32) (group) < FastPathLockGroupsPerBackend), \
222+
AssertMacro((uint32) (index) < FP_LOCK_SLOTS_PER_GROUP), \
223223
((group) * FP_LOCK_SLOTS_PER_GROUP + (index)))
224224

225225
/*
226226
* Given a slot index (into the whole per-backend array), calculated using
227227
* the FAST_PATH_SLOT macro, split it into group and index (in the group).
228228
*/
229229
#define FAST_PATH_GROUP(index) \
230-
(AssertMacro(((index) >= 0) && ((index) < FP_LOCK_SLOTS_PER_BACKEND)), \
230+
(AssertMacro((uint32) (index) < FP_LOCK_SLOTS_PER_BACKEND), \
231231
((index) / FP_LOCK_SLOTS_PER_GROUP))
232232
#define FAST_PATH_INDEX(index) \
233-
(AssertMacro(((index) >= 0) && ((index) < FP_LOCK_SLOTS_PER_BACKEND)), \
233+
(AssertMacro((uint32) (index) < FP_LOCK_SLOTS_PER_BACKEND), \
234234
((index) % FP_LOCK_SLOTS_PER_GROUP))
235235

236236
/* Macros for manipulating proc->fpLockBits */

src/backend/storage/lmgr/proc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ InitProcGlobal(void)
322322
}
323323

324324
/* Should have consumed exactly the expected amount of fast-path memory. */
325-
Assert(fpPtr = fpEndPtr);
325+
Assert(fpPtr == fpEndPtr);
326326

327327
/*
328328
* Save pointers to the blocks of PGPROC structures reserved for auxiliary

0 commit comments

Comments
 (0)