Fix Database::getHeaderInfo() signed-shift UB and use fixed-width Header types#558
Merged
Conversation
The big-endian assembly shifted unsigned char operands that promote to int, so bytes with the high bit set produced signed left-shift UB and sign-extended when widened to the Header fields. Cast each byte to uint32_t before shifting, via a local readBE32 helper that also removes the repetitive byte arithmetic. Header used platform-variable `unsigned long` (32-bit on LLP64 Windows, 64-bit on LP64), giving a cross-platform field-width mismatch. Switch the struct to fixed-width <cstdint> types (uint8_t / uint32_t). Fixes DB-02 and DB-03 from the code review.
SRombauts
added a commit
that referenced
this pull request
Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes two findings from the deep code review (DB-02, DB-03) in
Database::getHeaderInfo()and the publicHeaderstruct.DB-02 — signed left-shift UB in the big-endian assembly
The multi-byte header fields were assembled from
unsigned charoperands, which promote tointbefore the<< 24shift. For any byte with the high bit set this is signed-left-shift undefined behaviour, and theintresult was then sign-extended when widened to the (LP64)unsigned longfields.Each byte is now cast to
std::uint32_tbefore shifting, through a small localreadBE32helper that also collapses the 14 repeated 4-line blocks.DB-03 — platform-variable field widths
Headerusedunsigned long, which is 32-bit on LLP64 (Windows) but 64-bit on LP64 (Linux/macOS), so the struct layout and field widths differed across platforms. The struct now uses fixed-width<cstdint>types (std::uint8_t/std::uint32_t).Test plan
SQLiteCpp_tests(MSVC, Debug) — clean.ctest -C Debug— 100% passed, includingDatabase.getHeaderInfo.No public API behaviour changes; the parsed values are identical, only the field types and the (previously UB) assembly path change.