Skip to content

Commit b463288

Browse files
committed
Add Asserts to validate prevbit values in bms_prev_member
bms_prev_member() could attempt to access memory outside of the words[] array in cases where the prevbit was a number < -1 or > a->nwords * BITS_PER_BITMAPWORD + 1. Here we add the Asserts to help draw attention to bogus callers so we're more likely to catch them during development. In passing, fix wording of bms_prev_member's header comment which talks about how we expect the callers to ensure only valid prevbit values are used. Author: Greg Burd <[email protected]> Reviewed-by: David Rowley <[email protected]> Reviewed-by: Tom Lane <[email protected]> Discussion: https://postgr.es/m/2000A717-1FFE-4031-827B-9330FB2E9065%40getmailspring.com
1 parent 69f75d6 commit b463288

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/backend/nodes/bitmapset.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1343,7 +1343,7 @@ bms_next_member(const Bitmapset *a, int prevbit)
13431343
*
13441344
* Returns largest member less than "prevbit", or -2 if there is none.
13451345
* "prevbit" must NOT be more than one above the highest possible bit that can
1346-
* be set at the Bitmapset at its current size.
1346+
* be set in the Bitmapset at its current size.
13471347
*
13481348
* To ease finding the highest set bit for the initial loop, the special
13491349
* prevbit value of -1 can be passed to have the function find the highest
@@ -1379,6 +1379,10 @@ bms_prev_member(const Bitmapset *a, int prevbit)
13791379
if (a == NULL || prevbit == 0)
13801380
return -2;
13811381

1382+
/* Validate callers didn't give us something out of range */
1383+
Assert(prevbit <= a->nwords * BITS_PER_BITMAPWORD);
1384+
Assert(prevbit >= -1);
1385+
13821386
/* transform -1 to the highest possible bit we could have set */
13831387
if (prevbit == -1)
13841388
prevbit = a->nwords * BITS_PER_BITMAPWORD - 1;

0 commit comments

Comments
 (0)