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.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The new check still lets a platform file reach the shift-by--1 that the PR is meant to prevent. char_bit is accepted up to 255 (lib/platform.cpp:353), but getLimitsDefines() and Platform::unsignedCharMax() call max_value(char_bit + 1). When char_bit is 255, that value wraps to 0 in the std::uint8_t parameter. A debug build hits assert(bit > 0) in max_value() and aborts on any input file. A release build evaluates 1LL << -1. The limit has to leave room for char_bit + 1, for example bits >= std::numeric_limits<std::uint8_t>::max(). Fixing unsignedCharMax/getLimitsDefines would also work. A test for char_bit 255 is missing. Apart from this I found no other problem: widths from 65 to 255 bits are handled by the existing >= 64 guards.

Comments

# Comment By Where Status
1 char_bit 255 is accepted but max_value(char_bit + 1) then gets 0: assert or shift by -1 AI review lib/platform.cpp:353 🆕 New

@Nussu06, please fix, resolve or reply to the open and new comments: #1.

Testing

I built cppcheck and testrunner in pr/ with -O1 -g -fsanitize=undefined -fno-sanitize-recover=undefined. Asserts were enabled.

  • ./testrunner TestPlatform passed with 0 failures.
  • The full ./testrunner -q stopped in TestSymbolDatabase on a UBSan signed overflow at lib/valueflow.cpp:1175. This PR doesn't change that file. Because the build stops at the first UBSan error, I didn't get results for the suites after it.
  • Platform files with char_bit/size pairs of 64/2, 128/1 and 100/2 (widths from 100 to 200 bits) loaded and analysed a test file with no sanitizer or assert output.

Reproducer: char_bit 255 is accepted and then crashes

p255.xml:

<?xml version="1.0"?>
<platform><char_bit>255</char_bit><default-sign>signed</default-sign>
<sizeof><bool>1</bool><short>1</short><int>1</int><long>1</long><long-long>1</long-long><float>1</float><double>1</double><long-double>1</long-double><pointer>1</pointer><size_t>1</size_t><wchar_t>1</wchar_t></sizeof></platform>

e.c: int x;

$ ./cppcheck -q --platform=p255.xml e.c
cppcheck: lib/platform.h:56: static long long int Platform::max_value(uint8_t): Assertion `bit > 0' failed.
Aborted (core dumped)

Backtrace: Platform::max_value ← Platform::getLimitsDefines (lib/platform.cpp:414, max_value(char_bit+1)) ← createDUI ← Preprocessor::loadAllIncludes. With char_bit 128 the same file runs cleanly.


Automated review of 3639e1c by Claude. The pull request was built and tested locally.

Comment thread lib/platform.cpp
else if (std::strcmp(name, "sizeof") == 0) {
} else if (std::strcmp(name, "char_bit") == 0) {
const unsigned int bits = xmlTextAsUInt(node, error);
if (bits == 0 || bits > std::numeric_limits<std::uint8_t>::max())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

char_bit 255 passes this check, but getLimitsDefines() (max_value(char_bit+1), line 414) and Platform::unsignedCharMax() pass char_bit + 1 to max_value(std::uint8_t), where it wraps to 0. A debug build aborts on assert(bit > 0) for any input file. A release build evaluates 1LL << -1, the UB that this PR is meant to remove.

Repro: a platform file with <char_bit>255</char_bit> and all sizes set to 1, then cppcheck --platform=p255.xml e.c → lib/platform.h:56: ... Assertion 'bit > 0' failed.

Use bits >= std::numeric_limits<std::uint8_t>::max() here, or stop unsignedCharMax/getLimitsDefines from adding 1 in uint8_t. Please also add a test for 255.

@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit 3639e1c (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9102/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: -0.1% (http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9102/2026-12-02_08-06-52-3639e1ca7320_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

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