Repository navigation
Conversation
|
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
left a comment
There was a problem hiding this comment.
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 TestPlatformpassed with 0 failures.- The full
./testrunner -qstopped inTestSymbolDatabaseon a UBSan signed overflow atlib/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.
| 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()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good catch, that path was still reachable. I took the first option and rejected the maximum value itself, so char_bit is now limited to 1..254 and char_bit + 1 can no longer wrap to 0 in the std::uint8_t parameter of max_value().
Added invalid_config_file_5 with your 255 file. It fails on 3639e1c and passes now. Checked 254 with all sizes 1 as well: it loads and analyses with asserts enabled and no sanitizer output. Full testrunner is 5437 tests, 0 failed, and everything in platforms/ still loads.
|
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/ 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. |
|
Pushed b6a11cf for the review comment: On the ticket, could one of you create the trac ticket for this? I'll update the title to reference it once there's a number. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The PR doesn't fully fix the problem it targets. bitWidthFits() accepts a char_bit * sizeof product of up to 255, but getLimitsDefines() also calls max_value(short_bit+1) (USHRT_MAX) and max_value(int_bit+1) (UINT_MAX). If short_bit or int_bit is 255, that argument wraps to 0 in the std::uint8_t parameter. A debug build then aborts on assert(bit > 0) for any input file, and a release build evaluates 1LL << -1. Two examples: char_bit 85 with <short>3</short>, or char_bit 1 with <int>255</int>. This is the same + 1 wrap that C3 found for char_bit, but for short and int. The earlier char_bit 255 problem is fixed. Apart from this I found no new problems.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | A trac ticket should be created and the PR title updated | @dmcppcheck | general | 💬 Answered: The author asked the maintainers to create the ticket |
| 2 | char_bit 255 is accepted, and max_value(char_bit+1) wraps to 0 | AI review | lib/platform.cpp:353 |
✅ Fixed |
| 3 | short/int width 255 passes, but getLimitsDefines() max_value(short_bit+1)/(int_bit+1) wraps to 0 | AI review | lib/platform.cpp:334 |
🆕 New |
@Nussu06, please fix, resolve or reply to the open and new comments: #3.
Testing
I built cppcheck and testrunner in pr/ with the default Makefile build, which has asserts enabled. ./testrunner TestPlatform: 0 failures. A platform file with char_bit 127 and short/int size 2 (a width of 254) loads and analyses without problems.
Reproducer: a short or int width of 255 is accepted and then crashes
short255.xml:
<?xml version="1.0"?>
<platform><char_bit>85</char_bit><default-sign>signed</default-sign>
<sizeof><bool>1</bool><short>3</short><int>3</int><long>3</long><long-long>3</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>int255.xml: the same file with <char_bit>1</char_bit>, <short>1</short> and <int>/<long>/<long-long> set to 255.
e.c: int x;
$ ./cppcheck -q --platform=short255.xml e.c
cppcheck: lib/platform.h:56: static long long int Platform::max_value(uint8_t): Assertion `bit > 0' failed.
$ ./cppcheck -q --platform=int255.xml e.c
cppcheck: lib/platform.h:56: static long long int Platform::max_value(uint8_t): Assertion `bit > 0' failed.
The calls that fail are max_value(short_bit+1) and max_value(int_bit+1) in Platform::getLimitsDefines() (lib/platform.cpp:434 and :440).
Automated review of b6a11cf by Claude. The pull request was built and tested locally.
| // min_value()/max_value() undefined. | ||
| static bool bitWidthFits(unsigned int charBit, std::size_t size) | ||
| { | ||
| return size != 0 && charBit * size <= std::numeric_limits<std::uint8_t>::max(); |
There was a problem hiding this comment.
AI review
A width of exactly 255 passes this check, but getLimitsDefines() also calls max_value(short_bit+1) (USHRT_MAX, line 434) and max_value(int_bit+1) (UINT_MAX, line 440). With a std::uint8_t parameter, 255 + 1 wraps to 0. This is the same problem that was fixed for char_bit, but for short and int. A debug build aborts on assert(bit > 0) for any input file. A release build evaluates 1LL << -1.
Repro: a platform file with char_bit 85 and <short>3</short> (or char_bit 1 and <int>255</int>), then cppcheck --platform=p.xml e.c → lib/platform.h:56: ... Assertion 'bit > 0' failed.
One fix is to use < std::numeric_limits<std::uint8_t>::max() here, at least for short and int, or simply for all types. Another is to stop getLimitsDefines() from adding 1 in uint8_t. Please add tests for a short width and an int width of 255.
There was a problem hiding this comment.
Right, the same wrap applies to the short and int widths. I took the first option and made bitWidthFits() reject the maximum width itself, so no *_bit member can reach 255 and max_value(bit + 1) can no longer wrap to 0 for any type, not just short and int.
Both of your files are covered: invalid_config_file_6 is char_bit 85 with <short>3</short> and invalid_config_file_7 is char_bit 1 with <int>255</int>. Both fail on b6a11cf and pass now. char_bit 127 with short/int size 2 (254 bits) still loads and analyses with asserts enabled, everything in platforms/ and the built-in types still load, and the full testrunner is 5439 tests, 0 failed.
|
Test results for commit b6a11cf (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/ 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. |
|
Pushed 4598048 for the remaining comment: Still waiting on the trac ticket, could one of you create it? I will update the title with the number once it exists. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fixes for C3 and C8 hold, and the CLI now rejects the bad platform files. One new problem: loadFromXmlDocument() only reports the error. It still stores every size it read, still runs calculateBitMembers() and still sets type = Type::File, so a rejected file leaves a platform with a width of 0 in it. The CLI exits on the false return, but the GUI ignores it (gui/mainwindow.cpp:1188, settings.platform.loadFromFile(paths, ...)). A GUI project whose platform file has e.g. <int>0</int> is still analysed with int_bit == 0, which is the UB this PR is meant to prevent. Apart from this I found no new problems.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | A trac ticket should be created and the PR title updated | @dmcppcheck | general | 💬 Answered: The author asked the maintainers to create the ticket |
| 2 | char_bit 255 accepted; max_value(char_bit+1) wraps to 0 | AI review | lib/platform.cpp:353 |
✅ Fixed |
| 3 | short/int width 255 accepted; max_value(short_bit+1)/(int_bit+1) wraps to 0 | AI review | lib/platform.cpp:334 |
✅ Fixed |
| 4 | A rejected file still stores width-0/truncated members and type File; the GUI ignores the return value and analyses with them | AI review | lib/platform.cpp:401 |
🆕 New |
@Nussu06, please fix, resolve or reply to the open and new comments: #4.
Testing
I built cppcheck and testrunner in pr/ with the default Makefile (asserts enabled).
./testrunner TestPlatform: 0 failures (2 todos).- CLI: platform files with
char_bit254 and all sizes 1, and withchar_bit127 and int size 2 (254 bits), both load.--enable=allanalyses a test file with them and gives no assert. - CLI: a file with
<int>0</int>is rejected withcppcheck: error: unrecognized platform: 'bad.xml'., exit code 1.
A rejected file still leaves bogus widths in the Platform object
bad.xml:
<?xml version="1.0"?><platform><char_bit>8</char_bit><default-sign>signed</default-sign><sizeof><bool>1</bool><short>2</short><int>0</int><long>40</long><long-long>8</long-long><float>4</float><double>8</double><long-double>8</long-double><pointer>8</pointer><size_t>8</size_t><wchar_t>4</wchar_t></sizeof></platform>t.cpp:
#include "platform.h"
#include <iostream>
int main(){
Platform p;
bool ok = p.loadFromFile({"."}, "bad.xml");
std::cout << "ok=" << ok << " type=" << p.toString() << " int_bit=" << (int)p.int_bit << " long_bit=" << (int)p.long_bit << "\n";
}$ g++ -std=c++11 -I../pr/lib -I../pr/externals/tinyxml2 -I../pr/externals/simplecpp -I../pr/externals/picojson t.cpp ../pr/lib/*.o ../pr/externals/tinyxml2/tinyxml2.o ../pr/externals/simplecpp/simplecpp.o ../pr/frontend/frontend.o -o t && ./t
ok=0 type=platformFile int_bit=0 long_bit=64
long_bit is 64 only because 8 * 40 == 320 is truncated to uint8_t. The GUI path (gui/mainwindow.cpp:1188) ignores the return value and goes on with this object. I could not run the GUI itself: the PR doesn't change gui/, so I didn't build it.
Automated review of 4598048 by Claude. The pull request was built and tested locally.
| !bitWidthFits(char_bit, sizeof_long_double)) | ||
| error = true; | ||
|
|
||
| calculateBitMembers(); |
There was a problem hiding this comment.
AI review
When validation fails, only error is set. The sizes read from the file stay in the object, calculateBitMembers() still runs, and type is still set to Type::File. A rejected file therefore leaves e.g. int_bit == 0 (or a truncated width such as long_bit == 64 for 8 * 40) in the Platform.
The CLI exits on the false return, so it is safe. gui/mainwindow.cpp:1188 calls settings.platform.loadFromFile(paths, platform.toStdString()); and ignores the result, so a GUI project that names such a platform file still analyses with the bogus widths. That is the 1LL << (bit - 1) UB this PR is meant to remove.
Repro: load a file with <int>0</int> through Platform::loadFromFile → returns false, but type=platformFile int_bit=0.
Two possible fixes: validate before assigning anything and leave *this unchanged on error (load into a temporary Platform and assign it only on success), or check the return value in the GUI. A test that asserts the platform is unchanged after a rejected file would cover this.
|
Test results for commit 4598048 (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/ 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. |
Platform::loadFromXmlDocument reads char_bit and the type sizes from a
--platformXML file without a range check, while the*_bitmembers they are multiplied into arestd::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 evaluate1LL << (bit - 1)with bit 0 and shift by -1, andCheckTypeImpl::checkIntegerOverflowshifts bybits - 1== 0xffffffff. Theassert(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.con 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.