]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix bogus casting in BlockIdGetBlockNumber().
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 4 Mar 2022 00:03:17 +0000 (19:03 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 4 Mar 2022 00:03:55 +0000 (19:03 -0500)
commit18c04d1574568fd68e4f498483f3b10b11018c68
tree1e08a979d6c93014413548cb37862bbba3112fdc
parentcfbe87bd6f644e2b9b172516d94b971ed94dfc89
Fix bogus casting in BlockIdGetBlockNumber().

This macro cast the result to BlockNumber after shifting, not before,
which is the wrong thing.  Per the C spec, the uint16 fields would
promote to int not unsigned int, so that (for 32-bit int) the shift
potentially shifts a nonzero bit into the sign position.  I doubt
there are any production systems where this would actually end with
the wrong answer, but it is undefined behavior per the C spec, and
clang's -fsanitize=undefined option reputedly warns about it on some
platforms.  (I can't reproduce that right now, but the code is
undeniably wrong per spec.)  It's easy to fix by casting to
BlockNumber (uint32) in the proper places.

It's been wrong for ages, so back-patch to all supported branches.

Report and patch by Zhihong Yu (cosmetic tweaking by me)

Discussion: https://postgr.es/m/CALNJ-vT9r0DSsAOw9OXVJFxLENoVS_68kJ5x0p44atoYH+H4dg@mail.gmail.com
src/include/storage/block.h