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
I found no problems in the changed code. Under a -fsanitize=signed-integer-overflow build, all the cases the PR names no longer overflow, and the folded values stay the same as before (they wrap). One related site is still left: ValueFlowAnalyzer::writeValue in lib/vf_analyzers.cpp:374 does value->intvalue += (inc ? 1 : -1); for ++/--. So long long x = 0x7fffffffffffffff; x++; still makes cppcheck overflow a signed integer. The PR does not touch this path, and its tests don't cover it, so the claim that it gets rid of this kind of undefined behaviour is not complete. The same calculateArithmetic helper could be used there too.
Testing
- I built
cppcheckandtestrunnerfrompr/with the normal Makefile../testrunner TestMathLib TestValueFlow TestSymbolDatabase: 555 tests, 0 failed. - I built a second copy of
pr/withCXXFLAGS="-O1 -g -fsanitize=signed-integer-overflow,undefined -fno-sanitize-recover=signed-integer-overflow"and ran it on small inputs, one line per file:0x7fffffffffffffff + 1,0x7fffffffffffffff * 2,LLONG_MIN % -1in an expression and as a template argument,enum E { A = 0x7FFFFFFFFFFFFFFF, B, C };,x += 1/y *= 3afterx = LLONG_MAX, unary minus and/ -1ofLLONG_MIN, a conditionx == LLONG_MAXfollowed byx + 1, and a loop nearLLONG_MAX: no UBSan reports.- With
--debug, the folded values are as expected.+gives -9223372036854775808,*gives -2, and the enum givesB= LLONG_MIN andC= LLONG_MIN+1.(LLONG_MIN <=> 1)still folds to -1.
- A case that still overflows (not part of the PR's changes):
// sp/s9.cpp void k() { long long x = 0x7fffffffffffffff; x++; ++x; }
/work/pr_ubsan/cppcheck --std=c++20 --enable=all -q sp/s9.cpplib/vf_analyzers.cpp:374:29: runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long long int' #0 ValueFlowAnalyzer::writeValue(...) lib/vf_analyzers.cpp:374 #1 ValueFlowAnalyzer::update(...) lib/vf_analyzers.cpp:837 - I did not run the full testrunner under ASan+UBSan, did not test the Boost
int128_tbuild, and did not build base under UBSan.
Automated review of 8c80299 by Claude. The pull request was built and tested locally.
|
Good catch on About the trac ticket: could you create one for this? I'll change the title to "Fix #NNNNN (...)" as soon as it exists. |
|
This pull request has merge conflicts that must be resolved. |
|
Test results for commit fd6eddb (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9030/ 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. |
fd6eddb to
14439b1
Compare
|
Rebased on main. The conflict was with #9006, which added the compound assignment labels to I still don't see a trac ticket for this. Could one of you create it? I'll update the title to "Fix #NNNNN (...)" once it exists. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems in the code the PR changes. The ++/-- site in vf_analyzers.cpp from the earlier review now goes through calculateArithmetic, and long long x = LLONG_MAX; x++; no longer overflows under UBSan. The folded values are the same as before: they wrap.
Two signed overflows are still left in code the PR does not touch, so the claim that the overflow is gone is not complete:
- Unary minus in
lib/programmemory.cpp:2107(v.intvalue = -v.intvalue;), hit bylong long m = LLONG_MIN; long long n = -m;. #ifarithmetic inexternals/simplecpp/simplecpp.cpp:1298, hit by#if 0x7fffffffffffffff + 1 < 0.
Neither line is in the diff, so I can't comment on them inline.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | PR needs a trac ticket and an updated title | @dmcppcheck | general | |
| 2 | ++/-- in ValueFlowAnalyzer::writeValue still overflows a signed integer | AI review | general | ✅ Fixed |
@Nussu06, please fix, resolve or reply to the open and new comments: #1.
Testing
-
Normal build: I built
cppcheckandtestrunnerinpr/with the normal Makefile../testrunner TestMathLib TestValueFlow TestSymbolDatabase: 555 tests, 0 failed.- Full
./testrunner -q: 5420 tests, 0 failed.
-
UBSan build: I built a second copy of
pr/withCXXFLAGS="-O1 -g -fsanitize=signed-integer-overflow -fno-sanitize-recover=signed-integer-overflow -std=c++11". I ran it on files that cover:x++/--xatLLONG_MINandLLONG_MAX+=,*=and-=past the limits- an enum with
B = A - 1andA = LLONG_MIN - template arguments
LLONG_MAX + 1,LLONG_MIN % -1andLLONG_MAX * 3 <=>, a loop nearLLONG_MAX, and conditionsa == LLONG_MAXfollowed bya + 1/a * 2- reverse analysis (
x++; if (x == LLONG_MIN)) unsigned long longwrapping andintoverflow
None of the code the PR changes gave a UBSan report. The existing
integerOverflowwarning forA - 1in the enum still comes. -
Remaining overflows outside the diff (both are in code the PR does not change):
// scratch/u2.cpp void h() { long long m = -0x7fffffffffffffff - 1; long long n = -m; }
/work/pr_ub/cppcheck -q u2.cpplib/programmemory.cpp:2107:38: runtime error: negation of -9223372036854775808 cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself// scratch/t5.c #if 0x7fffffffffffffff + 1 < 0 int a; #endif
/work/pr_ub/cppcheck -q --enable=all t5.cexternals/simplecpp/simplecpp.cpp:1298:20: runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long long int' -
Not done: I did not build
testrunnerunder UBSan, did not test the Boostint128configuration, and did not buildbase/.
Automated review of 14439b1 by Claude. The pull request was built and tested locally.
calculate()in lib/calculate.h,MathLib::add/subtract/multiply/modandvalueFlowEnumValuefold integer constants from the checked code inMathLib::bigintwith plain signed arithmetic, so code likex = 0x7fffffffffffffff + 1;,A<0x8000000000000000 % -1>orenum E { A = 0x7FFFFFFFFFFFFFFE, B, C };makes cppcheck itself hit signed integer overflow (UBSan reports in calculate.h, mathlib.cpp and valueflow.cpp, and the% -1case can trap on x86-64). Added acalculateArithmetichelper that does +, - and * for signed integer types on the unsigned type and used it at those sites, andx % -1now returns 0, so folded values are the same as before and only the undefined behaviour is gone. With this, testrunner is clean under ASan+UBSan with signed-integer-overflow enabled (cmake/dynamic_analyzer_options.cmake still disables it with a TODO); I added asserts to TestMathLib::calculate and TestValueFlow::valueFlowCalculations plus a new TestSymbolDatabase::enum21, and they fail under UBSan without the fix. There is no trac ticket for this yet, could you create one? I will update the title to "Fix #NNNNN (...)" once it exists.