Repository navigation
Fix #15251 (fix out-of-range shifts for ValueType constness/volatileness bits) - #9055
Conversation
ValueType::constness and ValueType::volatileness have one bit per pointer level and were set and tested with 1 << pointer. The pointer depth comes from the analysed code, so a declaration with 32 or more '*' shifted an int by 32 or more, which is undefined behaviour. On x86 the shift count wraps and level 33 aliased level 1. Add qualifierBit(), which returns 0 for levels that do not fit in the mask, and use it at every site in symboldatabase.cpp.
|
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 PR misses one site. ValueType::str() in lib/symboldatabase.cpp still tests constness & (2 << p) and volatileness & (2 << p) in a loop over every pointer level. With 32 or more * this is the same undefined shift the PR fixes, and I reproduced it with UBSan through --debug-symdb. The other changed sites look correct: for levels of 32 and up, the masks ~qualifierBit(..) and qualifierBit(..) - 1 give all bits set. A UBSan build of the whole testrunner shows no shift errors and no test failures.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | PR should have a trac ticket and the title should reference it | @dmcppcheck | general | |
| 2 | ValueType::str() still uses 2 << p, which is undefined for pointer depth >= 32 |
AI review | lib/symboldatabase.cpp:8941 |
🆕 New |
@Nussu06, please fix, resolve or reply to the open and new comments: #1, #2.
Testing
Build: I built cppcheck and testrunner from pr/ with CXXFLAGS="-O1 -g -fsanitize=undefined -fno-sanitize-recover=shift".
Tests:
./testrunner TestSymbolDatabasepasses, including the newvariableDeepPointer.- The full
./testrunner -qrun has 5423 tests and 0 failures. UBSan reported one error, a signed overflow atlib/valueflow.cpp:1149, which is not a shift and is not related to this PR. It reported no shift errors.
Problem found: ValueType::str() still shifts out of range.
Test file deep.c:
int * const volatile ******************************** p;
void f(void) { int * const ********************************** q = 0; (void)q; }Command and output:
$ /work/pr/cppcheck --debug-symdb deep.c
...
lib/symboldatabase.cpp:8941:28: runtime error: left shift of 2 by 31 places cannot be represented in type 'int'
- Plain
--debugprinted no error on this file. - A declaration with 31
*gives no error either. Hereponly reaches 30, and2 << 30is allowed in C++14 and later. - Any declaration with 32 or more levels triggers the error. The same
str()is also called fromdebugSymbolDatabase()(--debug-normal/--debug-warningsvalue-type debug output).
New comments
lib/symboldatabase.cpp:8941
This site was missed. str() loops over every pointer level and tests constness & (2 << p) and volatileness & (2 << p). With 32 or more *, p reaches 31 and more, so the shift is the same undefined behaviour this PR fixes elsewhere.
I reproduced it with UBSan on int * const volatile ******************************** p; using cppcheck --debug-symdb:
lib/symboldatabase.cpp:8941:28: runtime error: left shift of 2 by 31 places cannot be represented in type 'int'
str() is also used in debugSymbolDatabase(). Using qualifierBit(p + 1) on both lines would fix it.
Automated review of ffa42a8 by Claude. The pull request was built and tested locally.
|
Test results for commit ffa42a8 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9055/ 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. |
|
hey can anyone create trac ticket for this pr, thanks. |
str() tested constness/volatileness with 2 << p, which is the same out-of-range shift for pointer depths of 32 and above. Reachable via --debug-symdb and debugSymbolDatabase().
|
Good catch on I reproduced your case first and confirmed it's gone: int * const volatile ******************************** p;
void f(void) { int * const ********************************** q = 0; (void)q; }With
On the trac ticket: I don't have an account there, so could one of you open it? Once there's a number I'll update the title and the description to reference it. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. The ValueType::str() site from the earlier AI review is fixed: it now uses qualifierBit(p + 1), and the new test checks the str() output. I grepped lib/ for other shifts on constness/volatileness and found none outside symboldatabase.cpp. At every changed site the result for depths below 32 is the same as before. For depths of 32 and more, ~qualifierBit(..) and qualifierBit(..) - 1 give all bits set, so the masks are still correct. Qualifiers at level 32 and deeper are no longer tracked, and the PR says so. The only thing still open is the trac ticket (C1), and nobody has created it yet.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | PR should have a trac ticket and the title should reference it | @dmcppcheck | general | |
| 2 | ValueType::str() still uses 2 << p, which is undefined for pointer depth >= 32 | AI review | general | ✅ Fixed |
@Nussu06, please fix, resolve or reply to the open and new comments: #1.
Testing
- I built
cppcheckandtestrunnerinpr/(the default Makefile uses-std=c++11) andcppcheckinbase/. This was a plain build, without sanitizers. ./testrunner TestSymbolDatabase TestValueType: 431 tests, 0 failed../testrunner -q: 5423 tests, 0 failed.- I wrote
scratch/t1.cppwith pointers of 32 and 33 levels. It covers:- overloads
g(int ***…)andg(const int ***…), called with anint * const ***…argument (findFunction/matchParameter) - a top-level
constparameter that the function assigns auto const a = pandconst auto *b = pwith a 33-levelp- a global
int * const ***… gp - const and non-const member overloads with 33-level parameters
- overloads
cppcheck --enable=all --inconclusive --check-level=exhaustive t1.cppprinted the same warnings with base and with the PR.--debug-symdbran fine with the PR.
Automated review of 17bb316 by Claude. The pull request was built and tested locally.
|
Test results for commit 17bb316 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9055/ 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. |
ValueType::constness and ValueType::volatileness in lib/symboldatabase.cpp have one bit per pointer level and are set and tested with
1 << pointer, but the pointer depth comes from the analysed code and is not limited, so a declaration likeint * const volatile ******************************** p;(33 levels) shifts by 33, which is undefined behaviour (UBSan:shift exponent 33 is too large for 32-bit type 'unsigned int'at symboldatabase.cpp:2674, and the same at the other sites). On x86 the shift count wraps, so level 33 aliases level 1 andp->valueType()->isConst(0)returns true for that declaration. Fixed by adding a small qualifierBit() helper that returns 0 for levels 32 and above (those levels are not tracked) and using it at every shift site in the file, so results for depths below 32 are unchanged. The new TestSymbolDatabase::variableDeepPointer fails without the change and passes with it, testrunner shows no new failures, and a-fsanitize=shiftbuild no longer reports anything for deep pointer declarations. There is no trac ticket for this yet, could one of you create it? I'll update the title to reference it.