Re: Undefined behavior detected by new clang's ubsan
Alexander Law <exclusion@gmail.com>
From: Alexander Lakhin <exclusion@gmail.com>
To: John Naylor <johncnaylorls@gmail.com>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>, Tom Lane <tgl@sss.pgh.pa.us>
Date: 2026-01-21T20:00:00Z
Lists: pgsql-hackers
Hello John, 21.01.2026 12:05, John Naylor wrote: > As for the rest of the proposed fixes, most seem okay, but I have some nits: Thank you for spending time on this! I agree with all of your changes (except for one noted below) -- didn't mean to propose committable changes, just wanted to show the fixes that allowed check-world to pass. > heaptoast.c > memcpy(VARDATA(result) + > - (curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt, > + (int)(curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt, > > Not sure about this one. It would be better if we reversing the > operands allowed us to avoid overflow in the first place: > > - (curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt, > + chcpystrt + (curchunk * TOAST_MAX_CHUNK_SIZE - sliceoffset) > > Does that silence the warning? Unfortunately, no -- I still got: heaptoast.c:771:17: runtime error: addition of unsigned offset to 0x78120673fac6 overflowed to 0x78120673fa04 SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior heaptoast.c:771:17 Best regards, Alexander
Commits
-
Future-proof sort template against undefined behavior
- 7467041cde9e 19 (unreleased) landed
-
Fix various instances of undefined behavior
- 176dffdf7d2a 19 (unreleased) landed
- fc56793c9ae5 14.21 landed
- b5e1cd2fdca1 18.2 landed
- 73ac2b37401d 16.12 landed
- 6b81a1c7c905 15.16 landed
- 1662cd0cb7ae 17.8 landed