Skip to content

fix out-of-range shifts in ValueFlow castValue and truncateIntValue - #9073

Open
Nussu06 wants to merge 1 commit into
cppcheck-opensource:mainfrom
Nussu06:valueflow-truncate-shift-width
Open

Nussu06 wants to merge 1 commit into
cppcheck-opensource:mainfrom
Nussu06:valueflow-truncate-shift-width

Conversation

@Nussu06

@Nussu06 Nussu06 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

ValueFlow::castValue and ValueFlow::truncateIntValue in lib/vf_common.cpp shift by a bit width taken from ValueType::getSizeOf, which is 0 for integer types of unknown size and above 64 for records and containers, so a prefix ++ or -- on a struct or container variable shifts by a count far outside 64 bits (UBSan at vf_common.cpp:104, and for a 12 byte struct the value is truncated to 32 bits), and a mixed-sign comparison of two unknown-size types such as int_fast8_t and uint_fast8_t shifts by -1 and zeroes the value, which reports int_fast8_t a = 0; uint_fast8_t b = 10; if (a == b) as always true. Both helpers now return the value unchanged when the width is 0 or 64 bits and above, matching the sz < 8 guard the other truncateIntValue callers already have; widths of 1 to 63 bits are handled exactly as before. The new asserts in TestValueFlow::valueFlowIncDec and valueFlowUnsigned fail on main and pass with the change, the full testrunner passes in a normal build and under -fsanitize=undefined, and the touched files are unchanged by uncrustify. I do not have Trac access, so could one of you create a ticket for this? I will update the title to Fix #NNNNN (...) once it exists.

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

@Nussu06

Nussu06 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

hey can anyone create trac ticket for this pr, thanks.

@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

No new problems found. The two guards stop the out-of-range shifts and do not change results for widths of 1–63 bits; with value_size == 8 the old code already left the value unchanged, so only sizes of 0 and above 8 (and bit == 0 in castValue) behave differently. The mixed-sign comparison of unknown-size types such as int_fast8_t == uint_fast8_t is no longer reported as always true. The trac ticket that C1 asked for does not exist yet.

Comments

# Comment By Where Status
1 PR needs a trac ticket and the title should be updated @dmcppcheck general ⚠️ Open

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

Testing

  • Builds: make -j8 cppcheck testrunner built cleanly in pr/, and make -j8 cppcheck built cleanly in base/.
  • Changed suites: ./testrunner TestValueFlow TestCondition TestOther TestType ran 433 tests and none failed.
  • Full testrunner: ./testrunner -q ran 5425 tests and none failed.
  • Comparison with base: I ran both binaries on a scratch file with cppcheck -q --enable=all --check-level=exhaustive a.cpp:
    • int_fast8_t a = 0; uint_fast8_t b = 10; if (a == b): base reports Condition 'a==b' is always true, a false positive; the PR reports always false, which is correct.
    • int_fast16_t a = -1; uint_fast16_t b = 5; if (a == b): base reports always true, which is wrong; the PR reports always false.
    • These cases came out the same on both binaries, and correctly:
      • unsigned char 255 wraps to 0 after ++.
      • signed char 127 becomes -128 after ++.
      • long long -1 compared with unsigned long long 0xffff…ffff is always false.
      • int_fast8_t -1 compared with unsigned int 0xffffffff is always true.
    • ++ on a 12-byte struct with operator++ gave no warnings and did not crash.
  • Not done: I did not run a UBSan build.

Automated review of 3b275f0 by Claude. The pull request was built and tested locally.

@dmcppcheck

Copy link
Copy Markdown
Contributor

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

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9073/
packages: 1000
Differing warnings: 0 (+0 -0)
Timing: +0.0% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9073/2026-11-25_16-14-05-3b275f070cfc_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