Skip to content

validate char_bit and type sizes in Platform::loadFromXmlDocument - #9102

Open
Nussu06 wants to merge 1 commit into
cppcheck-opensource:mainfrom
Nussu06:platform-bit-width-validation
Open

Nussu06 wants to merge 1 commit into
cppcheck-opensource:mainfrom
Nussu06:platform-bit-width-validation

Conversation

@Nussu06

@Nussu06 Nussu06 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Platform::loadFromXmlDocument reads char_bit and the type sizes from a --platform XML file without a range check, while the *_bit members they are multiplied into are std::uint8_t. A file with char_bit 32 and <int>8</int> makes int_bit 32 * 8 == 256, which truncates to 0, and a char_bit or a size of 0 gives a zero width directly; Platform::min_value()/max_value() then evaluate 1LL << (bit - 1) with bit 0 and shift by -1, and CheckTypeImpl::checkIntegerOverflow shifts by bits - 1 == 0xffffffff. The assert(bit > 0) in those helpers only fires in a debug build, so a release build keeps analysing the code with bogus type limits. The values are now validated where the file is read, so such a file is rejected with the existing "unrecognized platform" error, the same as a non-numeric value already is.

I verified this with a UBSan build. Running cppcheck --platform=p.xml --enable=all t.c on a platform file with char_bit 32 and <int>8</int> reported "shift exponent -1 is negative" at platform.h:52 and :59, "shift exponent 4294967295 is too large for 64-bit type" at checktype.cpp:200 and "shift exponent -1 is negative" at vf_common.cpp:370 before the change, and after it the file is rejected with no sanitizer output. Everything in platforms/ and the built-in platform types still load, the full testrunner passes, and both changed files are clean under selfcheck.

One behaviour change worth calling out: valid_config_file_4 asserted that a platform file with char_bit and every size set to 0 loads successfully, which is the case this rejects, so it is now invalid_config_file_3. invalid_config_file_4 covers the char_bit and sizeof product that does not fit.

Could one of you create a trac ticket for this? I will update the title to reference it.

@dmcppcheck

Copy link
Copy Markdown
Contributor

There should probably be a trac ticket for this PR. Please create it, or ask us to create it. Then the PR title should be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants