Skip to content

Fix #15251 (fix out-of-range shifts for ValueType constness/volatileness bits) - #9055

Merged
danmar merged 2 commits into
cppcheck-opensource:mainfrom
Nussu06:valuetype-qualifier-shift
Nov 22, 2026
Merged

danmar merged 2 commits into
cppcheck-opensource:mainfrom
Nussu06:valuetype-qualifier-shift

Conversation

@Nussu06

@Nussu06 Nussu06 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

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 like int * 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 and p->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=shift build 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.

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.
@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 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 ⚠️ Open
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 TestSymbolDatabase passes, including the new variableDeepPointer.
  • The full ./testrunner -q run has 5423 tests and 0 failures. UBSan reported one error, a signed overflow at lib/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 --debug printed no error on this file.
  • A declaration with 31 * gives no error either. Here p only reaches 30, and 2 << 30 is allowed in C++14 and later.
  • Any declaration with 32 or more levels triggers the error. The same str() is also called from debugSymbolDatabase() (--debug-normal/--debug-warnings value-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.

@dmcppcheck

Copy link
Copy Markdown
Contributor

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/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: +0.0% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9055/2026-11-21_06-50-21-ffa42a8d6ab2_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.

@Nussu06

Nussu06 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

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().
@Nussu06

Nussu06 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Good catch on ValueType::str(), I missed it. 2 << p is 1 << (p + 1), so it's now qualifierBit(p + 1) on both lines. Pushed in 17bb316.

I reproduced your case first and confirmed it's gone:

int * const volatile ******************************** p;
void f(void) { int * const ********************************** q = 0; (void)q; }

With -O1 -g -fsanitize=undefined -fno-sanitize-recover=shift, cppcheck --debug-symdb deep.c no longer reports anything, and the value types still print as before (signed int * const volatile * * ...). ./testrunner -q is 5423 tests, 0 failures, and the only UBSan hit left is the signed overflow at valueflow.cpp:1149 that you also saw, which is unrelated to this.

variableDeepPointer now also asserts the full str() output for the 33 level declaration, so that site is covered by the test too.

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 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

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 ⚠️ Open
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 cppcheck and testrunner in pr/ (the default Makefile uses -std=c++11) and cppcheck in base/. This was a plain build, without sanitizers.
  • ./testrunner TestSymbolDatabase TestValueType: 431 tests, 0 failed.
  • ./testrunner -q: 5423 tests, 0 failed.
  • I wrote scratch/t1.cpp with pointers of 32 and 33 levels. It covers:
    • overloads g(int ***…) and g(const int ***…), called with an int * const ***… argument (findFunction/matchParameter)
    • a top-level const parameter that the function assigns
    • auto const a = p and const auto *b = p with a 33-level p
    • a global int * const ***… gp
    • const and non-const member overloads with 33-level parameters
  • cppcheck --enable=all --inconclusive --check-level=exhaustive t1.cpp printed the same warnings with base and with the PR. --debug-symdb ran fine with the PR.

Automated review of 17bb316 by Claude. The pull request was built and tested locally.

@dmcppcheck

Copy link
Copy Markdown
Contributor

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/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: +0.3% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9055/2026-11-21_18-03-28-17bb316206be_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.

@danmar danmar changed the title fix out-of-range shifts for ValueType constness/volatileness bits Fix #15251 (fix out-of-range shifts for ValueType constness/volatileness bits) Nov 22, 2026
@danmar
danmar merged commit b0fcf90 into cppcheck-opensource:main Nov 22, 2026
74 checks passed
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.

3 participants