| From: | Lev Nikolaev <lev(dot)nikolaev(at)tantorlabs(dot)com> |
|---|---|
| To: | "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org> |
| Subject: | [PATCH] Avoid signed overflow in bms_prev_member assertion |
| Date: | 2026-08-24 22:40:36 |
| Message-ID: | 3e991e23-2260-459f-b3d0-e84fb48d353a@tantorlabs.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
|Hi, Hackers! I noticed that the range assertion in bms_prev_member()
performs the multiplication in signed int arithmetic before converting
its result to unsigned int: (unsigned int) (a->nwords *
BITS_PER_BITMAPWORD) For a Bitmapset capable of containing INT_MAX,
nwords is INT_MAX / BITS_PER_BITMAPWORD + 1. On a 64-bit build this is
33554432, and multiplying it by 64 cannot be represented by int. A small
UBSan test reports: runtime error: signed integer overflow: 33554432 *
64 cannot be represented in type 'int' PostgreSQL normally builds with
-fwrapv, so this does not currently produce an incorrect assertion
result on GCC/Clang builds using the standard project flags.
Nevertheless, the expression relies on signed wraparound despite the
surrounding code intentionally using unsigned arithmetic to handle the
INT_MAX boundary. The attached patch casts nwords before the
multiplication, matching the calculation a few lines below: (unsigned
int) a->nwords * BITS_PER_BITMAPWORD I did not add a regression test
because reaching this boundary requires allocating an approximately 256
MB Bitmapset, and the affected expression is only present in
assertion-enabled builds. The modified bitmapset.c compiles successfully
and git diff --check passes. |
||
--
Best regards,
Lev Nikolaev,
Tantor Labs LLC,
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-Fix-signed-overflow-in-bms_prev_member-assert.patch | text/x-patch | 586 bytes |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | David Rowley | 2026-08-24 22:43:56 | Re: Add bms_offset_members() function for bitshifting Bitmapsets |
| Previous Message | Melanie Plageman | 2026-08-24 22:30:46 | Re: Preserve statistics targets with ALTER TABLE ALTER COLUMN TYPE |