Repository navigation
Conversation
a1c4a25 to
8dc01be
Compare
8dc01be to
d37f6b1
Compare
|
I have created this ticket: https://trac.cppcheck.net/ticket/15000 |
|
I will update the commit message with Fix #15000 ... |
d37f6b1 to
3e3fa63
Compare
3e3fa63 to
2d5f3a7
Compare
|
The PR title should be updated. The commit message will not be shown in the git log but the PR title will. |
Done. PR title updated to "Fix #15000 CheckCondition: detect compareValueOutOfTypeRange for bounded cast/arithmetic expressions" ; let me know if better convention would apply here. |
Thanks! I don't have a strict pattern that must be followed but I typically put parentheses around the ticket title. Most important is that you indicated which ticket it solves.. |
|
I have a feeling that some of the logic should be performed in ValueFlow and not in the check. Currently, ValueFlow only seems to handle |
I agree that checkers should have some nice way to determine min/max values for an expression. If that is put in |
I meant that ValueFlow should determine that the condition is always true/false, instead of having complex parsing logic (in this case, for casts and arithmetic) in the checker. |
2d5f3a7 to
a8d48d3
Compare
1f38959 to
1ebcbe5
Compare
1ebcbe5 to
1de33f6
Compare
|
I rebased against main (no conflicts). |
1de33f6 to
8502e53
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems in this version. In my tests, every result that differs from base is a correct new warning or a wrong base warning that is now gone.
New results in this version that are correct:
- The PR's reproducer pattern (with
stdint.hnames and a>= 4294967294LLvariant) is now reported as always false. (uint16_t)(uint8_t)x > 255is now reported as always false.- On avr8, the PR removes base's wrong results for
us + 1 == 0,us == -1andsigned char c == 0xFFFFu. - A
uint8_tdecremented in place no longer gets a false "always false" warning. _Bool b = 5; b == 1is now reported as always true. Base reported it as always false, which was wrong.
Problems that comments.md already lists are not repeated here.
Testing
Build: make -j8 cppcheck testrunner in pr/ and make -j8 cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestType, TestStl, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0.
Python CLI tests: in test/cli/other_test.py, the tests matching -k "15000 or missing_include or unique_error" give 5 passed. The new floating_strict… and unsigned_loop_index… tests give 3 passed.
Base vs PR comparison: I ran both builds with --enable=style,warning --platform=unix64 and compared the output. For e.c I also used --platform=avr8. The test files are in /work/scratch:
a.c: conversions on assignment. Float to int, out-of-range values forunsigned char,signed char,short,unsignedand_Bool, auint8_twrapping on++, and a 64-bit value cast toint. Results are the same as base, except that_Bool b = 5; b == 1is now correctly reported as always true (base said always false).b.c: casts with arithmetic and comparisons. There is one correct new warning:(uint16_t)(uint8_t)x > 255is always false. Base's false "always false" foruint8_t c = (uint8_t)x; c += 10; if (c < 10)is gone.c.c,h.c:forloops. These cover&&/||conditions, start values that are negative or come from a condition or a cast, and values after the loop. They give the same warnings as base. SomenegativeIndexmessages now name the condition that the value comes from.d.c: shift counts. Shift counts from ternaries and conditions give the same results as base.e.c:compareValueOutOfTypeRangewith promotions and mixed signs. On unix64 the results are the same as base. On avr8 (16-bitint) the PR removes base's wrong warnings forus + 1 == 0,us == -1andsigned char c == 0xFFFFu, and correctly addsunsignedLessThanZeroforus*2 < 0.f.c: values from function arguments and casts through local variables. Same results as base.g.c: the PR's reproducer pattern and variations. The new always-false results for((int64_t)((uint32_t)x)) - 1LL > 4294967295LLand>= 4294967294LLare correct. There are no new false positives.(int32_t)x - 65535 > 0with auint16_txis still not reported, the same as base. This is a limitation, not a regression.
Real code: I also ran both builds on test/cfg/{std.c,std.cpp,posix.c,gnu.c,windows.cpp} (with --library), lib/mathlib.cpp, lib/token.cpp and tinyxml2.cpp. The output was the same for base and the PR, and the run times were about the same: std.c took 5 s with base and 4 s with the PR, std.cpp took 7 s and 6 s.
Automated review of 56f0fa9 by Claude. The pull request was built and tested locally.
|
Test results for commit 56f0fa9 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8819/ 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. |
|
can you please comment on the "Still open" : Still open:
Fixed (verified by running the reproducers):
|
|
I do have a good feeling about this PR. And if you feel some AI reviews are out-of-scope for this PR then feel free to suggest that we create tickets in Trac for some AI reviews... it's important that we don't introduce false positives but false negatives are acceptable if there is a Trac ticket.. |
|
This pull request has merge conflicts that must be resolved. |
bd99e76 to
28017f4
Compare
|
I can do a couple of iterations to improve, most of these AI reviews were meaningful. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
New false negative: a narrowing (or same-size) integral cast or assignment of a value whose range is already bounded by conditions now loses that range. Base reports these conditions, and the PR does not. For example, if (x < 0 || x > 100) return; if ((uint8_t)x > 200) is no longer reported as always false. The same happens with (int8_t)x < 0, (short)x > 200, static_cast<uint8_t>(x), and with assignments like uint8_t c = x; and short s = (short)x;.
The cause is that the cast handles each value on its own. x has the excluded ranges !<=-1 and !>=101, and each one on its own does not survive narrowing. Only the pair proves that the range fits. So the cast token keeps only the type bounds, {!<=-129,!>=128} or {!<=-1,!>=256}, and the <=100 bound is lost. This is a common clamp-then-narrow pattern, so it should be fixed or tracked in a Trac ticket, as danmar suggested for false negatives.
I found no new false positives or crashes. The items that were still open in C108 are fixed in this version:
- the containerOutOfBounds resize guard;
a[i + 1]with an unsigned loop index;- plain
charcompared with0xFFor127, and the #11592 test is restored.
Earlier comments
57 earlier comments, 0 open
Testing
Build: make -j8 cppcheck testrunner in pr/ and make -j8 cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestType, TestStl, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0.
CLI tests: pytest other_test.py -k "15000 or floating or unsigned_loop or bounded or missing_include" gives 7 passed.
New false negative: bounded value through a narrowing cast or assignment (t3.c)
#include <stdint.h>
void use(int);
void d3(int x) { if (x < 0 || x > 100) return; if ((int8_t)x < 0) use(3); }
void e1(int x) { if (x < 0 || x > 100) return; if ((uint8_t)x > 200) use(1); }
void e2(int x) { if (x < 0 || x > 100) return; if ((short)x > 200) use(2); }
void e3(int x) { if (x < 0 || x > 100) return; if ((long long)x > 200) use(3); }
void e4(int x) { if (x < 0 || x > 100) return; uint8_t c = x; if (c > 200) use(4); }Command: cppcheck -q --enable=style,warning --platform=unix64 --template='{line}:{id}:{message}' t3.c
Base:
3:knownConditionTrueFalse:Condition '(int8_t)x<0' is always false
4:knownConditionTrueFalse:Condition '(uint8_t)x>200' is always false
5:knownConditionTrueFalse:Condition '(short)x>200' is always false
6:knownConditionTrueFalse:Condition '(long long)x>200' is always false
7:knownConditionTrueFalse:Condition 'c>200' is always false
PR: only the line 6 warning, the widening cast.
--debug for line 3 shows:
x {!<=-1,>=0,!>=101,<=100}( always {!<=-129,!>=128}
The C++ versions behave the same: static_cast<uint8_t>(x) > 200, short s = (short)x; s > 200, and unsigned char a[4]; a[0] = x; a[0] == 200. Base reports all three and the PR reports none of them, also with --check-level=exhaustive.
Earlier findings checked again: these reproducers now give correct results (no false positive, or the expected warning):
- containerOutOfBounds resize guard;
- unsigned loop index
a[i+1]; char c == 0xFFandc > 127;wchar_ton win64;- flag enum
0x80000000; ||loop zerodiv;- float uninitvar;
- floatConversionOverflow;
&&loop off-by-one;- negativeIndex with
if (x >= 0) {}; - negative value cast to unsigned;
y -= 1after a cast.
Other comparisons: about 40 cast and arithmetic cases (t1.c, t2.c) gave no new false positives. The PR removes several wrong base results, for example:
(unsigned)x > 10uwithxin [-5, 5];unsigned char c = (unsigned char)x; c++; c == 0;y + 65536u == 0withy = (uint16_t)x * 65536u.
Automated review of 28017f4 by Claude. The pull request was built and tested locally.
655683a to
fc51719
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
New false negative: the clamp-then-narrow fix only works when the cast is inside the branch of a separate if. When the range check and the narrowing cast are in the same && chain, the same ?: condition, or the same while condition, the cast keeps only its type bounds. Base reports these as always false and the PR does not:
if (x >= 0 && x <= 100 && (uint8_t)x > 200)- the same with
(int8_t)x < 0and(short)x > 200 if (x < 0 || x > 100 || (uint8_t)x > 200)(x >= 0 && x <= 100) ? (uint8_t)x > 200 : 0while (x >= 0 && x <= 100 && (uint8_t)x > 200)
The cause is isConditionFactActiveAtUse(): it needs conditionBranchAtUse() to put the use in a true or false branch of an if/while. A use in the right-hand operand of &&/||/?: is not in such a branch, so the facts from the condition are rejected. This is only a false negative.
The C113 case (if (x < 0 || x > 100) return; then a narrowing cast or assignment) is fixed in this version. I found no new false positives, crashes or slowdowns.
Earlier comments
63 earlier comments, 0 open
Testing
Build: make -j4 cppcheck testrunner in pr/ and make -j4 cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestType, TestStl, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0.
CLI tests: pytest other_test.py -k "15000 or floating or unsigned_loop or bounded or missing_include or cast" gives 8 passed.
Regression: narrowing cast in the same condition as the range check (n3.c)
#include <stdint.h>
void use(int);
void a8(int x) { if (x >= 0 && x <= 100 && (uint8_t)x > 200) use(9); }
void c1(int x) { if (x >= 0 && x <= 100 && (int8_t)x < 0) use(1); }
void c2(int x) { if (x >= 0 && x <= 100 && (short)x > 200) use(2); }
void c3(int x) { if (x >= 0 && x <= 100 && (long long)x > 200) use(3); }
void c4(int x) { if (x < 0 || x > 100 || (uint8_t)x > 200) use(4); }
void c5(int x) { if (x >= 0 && x <= 100) if ((uint8_t)x > 200) use(5); }
int c6(int x) { return x >= 0 && x <= 100 && (uint8_t)x > 200; }Command: cppcheck -q --enable=style,warning --platform=unix64 --template='{line}:{id}:{message}' n3.c
Base reports lines 3 to 9 as always false. PR output:
6:knownConditionTrueFalse:Condition '(long long)x>200' is always false
8:knownConditionTrueFalse:Condition '(uint8_t)x>200' is always false
--debug for line 3 with the PR:
( always {!<=-1,!>=256}
x {>=0,!<=-1,<=100,!>=101}
The excluded bounds of x are there, but the cast does not combine them.
The ternary and loop forms behave the same way (n4.c):
int t1(int x) { return (x >= 0 && x <= 100) ? (uint8_t)x > 200 : 0; }
void w1(int x) { while (x >= 0 && x <= 100 && (uint8_t)x > 200) x++; }Base reports (uint8_t)x>200 as always false for w1 and gives duplicateValueTernary for t1. The PR reports neither.
C113 check (n1.c, about 20 clamp-then-narrow variants):
- All of C113's cases are reported again:
(int8_t),(uint8_t),(short), anduint8_t c = x. - The PR also fixes several wrong base results where the clamped range does not fit the destination type. Examples:
[0,300]cast with(uint8_t)and compared> 200;[-5,100]cast with(uint8_t)and compared== 251;[0,255]cast with(int8_t)and compared< 0;[-100,100]cast with(unsigned)and compared> 100u. unsigned x <= 100with(int8_t)x < 0is a correct new warning.
Possible false positives (n2.c, n4.c): facts that are later invalidated gave no new false positives. I tried an assignment after the clamp, x *= 3, x++ in a loop, a conditional reassignment, facts from only one branch, and per-call function argument values.
Earlier reproducers (old.c, run on unix64 and win64): the earlier findings are fixed. I re-ran:
~(uint32_t)x;char == 0xFF;y -= 1after a cast;signed char i++;- an unsigned loop index used as
a[i + 1]; - an
&&loop; (char)0;- the
0x80000000enum; wchar_t == 0xFFFF.
Real code: base and PR on test/cfg/std.c, test/cfg/posix.c, lib/mathlib.cpp and lib/token.cpp give identical output (1542 lines each) in the same time (81.5 s for each).
Automated review of fc51719 by Claude. The pull request was built and tested locally.
fc51719 to
1d33807
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
New false positives when a signed variable is compared with an unsigned constant of int rank or higher. With the PR, the literal in x >= 0x80000000u or x == 0xFFFFFFFFu has the value it has in the unsigned common type (2147483648, 4294967295). Base had -2147483648 and -1. The condition handler still gives these values to the int variable unchanged. The facts it derives can never hold for an int: x {!<=4294967294,>=4294967295}. So common checks like if (x >= 0x80000000u) { if (x < 0) … } now get oppositeInnerCondition ("dead code"), and if (x < 0x80000000u) return; if (x == -5) gets "always false". Both conditions are actually always true. In one case the PR turns base's correct "always true" into "always false". Base already has wrong results for some mixed-sign conditions, but the cases below are new or have the opposite answer.
The unit tests pass. C115 is fixed: &&, || and while uses are now reported. I checked 123 other new knownConditionTrueFalse results from generated cast/arithmetic code by compiling and running the code, and all of them were correct.
Earlier comments
68 earlier comments, 0 open
Testing
Build: make -j8 cppcheck testrunner in pr/ and make -j8 cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestType, TestStl, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0.
CLI tests: pytest other_test.py -k "15000 or floating or unsigned_loop or bounded or cast" gives 4 passed.
Differential test with an oracle: I generated 30 files with 150 functions each. Each function has a random guard on x, then nested casts, arithmetic and ~ on x, then a comparison. I ran base and the PR (--platform=unix64). For every knownConditionTrueFalse that only the PR reports, I compiled the file with gcc -fwrapv and ran the function over about 4000 values of x, including the type limits.
- 123 of the new results were correct.
- 3 were wrong. All three have a signed
xcompared with an unsigned constant in the guard.
Reduced reproducer (r.c):
void use(int);
void r1(int x) { if (x >= 0x80000000u) { if (x < 0) use(1); } }
void r2(int x) { if (x < 0x80000000u) return; if (x == -5) use(2); }
void r5(int x) { if (x == 0xFFFFFFFFu) { if (x == -1) use(5); } }
void m6(int x) { if (x < 4294967295u || x >= 127) return; if (x * 255 - 150 < 65535) use(6); }
void m9(int x) { if (x < 4294967295u) return; if (x == -1) use(9); }
void m2(int x) { if (x < 1u || x >= 4294967295u) return; if (~x != 1) use(3); }Command: cppcheck -q --enable=style,warning --platform=unix64 --template='{line}:{id}:{message}' r.c
PR, line by line:
- r1:
oppositeInnerCondition: Opposite inner 'if' condition leads to a dead code block.Base gives nothing. Forint x,x >= 0x80000000uholds exactly whenx < 0. - r2:
Condition 'x==-5' is always false. Base gives nothing. It is true forx == -5. - r5:
oppositeInnerCondition. Base saysCondition 'x==-1' is always true, which is correct. - m6:
Condition 'x*255-150<65535' is always false. Base says always true, which is correct: onlyx == -1reaches it, and that gives -405. - m9:
Condition 'x==-1' is always false. Base gives nothing. It is always true. - m2:
Condition '~x!=1' is always true. Base gives nothing. It is false forx == -2.
--debug for m9:
- PR:
4294967295u always 4294967295andx {!<=4294967294,>=4294967295} - base:
4294967295u always -1andx {!<=-2,>=-1}
C115 check: x >= 0 && x <= 100 && (uint8_t)x > 200, (int8_t)x < 0, the || form and the while form are reported again. With a ternary ((x>=0&&x<=100) ? (uint8_t)x>200 : 0), neither version reports the condition. Base's duplicateValueTernary is still lost, as C114 already noted.
Possible false positives: I checked facts that are made invalid before the use:
x = 250,x *= 3or++xinside the&&chain;set(&x)or*p = 250after the check;- a write in a loop body, a
gotoback edge, aswitchfall-through, and theelsebranch.
None gave a false positive.
Automated review of dcb0cec by Claude. The pull request was built and tested locally.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The mixed-sign fix for C116/C117 is incomplete, and the PR still reports false positives that base does not. castConstantToOperandType() returns early unless the unsigned constant is a literal token. When the constant comes from an unsigned variable, a const variable, a cast like (unsigned)-1, or ~0u, the signed variable still gets an impossible value: int x becomes always 4294967295. With the PR this gives:
oppositeInnerConditionand "always false" onx == -1, which is always true; base says "always true", which is correct.integerOverflowCondfalse positives.- The real
zerodivcondin10 / (x + 1)is no longer reported.
The literal cases from C117 (r1, r2, r5, m9) are fixed now. C116's ~x != 1 case (m2) is still wrongly reported as always true.
All the unit tests and the PR's CLI tests pass.
Earlier comments
66 earlier comments, 2 open
@jfdeverge, please fix, resolve or reply to the open comments: #65, #66.
Testing
Build: make -j8 cppcheck testrunner in pr/ and make -j8 cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestType, TestStl, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0.
CLI tests: pytest other_test.py -k "15000 or floating or unsigned_loop or bounded or cast" gives 4 passed.
C116/C117 reproducers (r.c, --platform=unix64):
- r1, r2, r5 and m9 are now correct.
- m2 (
if (x < 1u || x >= 4294967295u) return; if (~x != 1)) is still reported asCondition '~x!=1' is always true.
New: the fix only covers literal constants (v.c)
void use(int);
void v1(int x) { unsigned u = 0xFFFFFFFFu; if (x == u) { if (x == -1) use(1); } }
void v2(int x) { const unsigned M = 0xFFFFFFFFu; if (x == M) { if (x == -1) use(2); } }
void v3(int x) { if (x == (unsigned)-1) { if (x == -1) use(3); } }
void v4(int x) { if (x == ~0u) { if (x == -1) use(4); } }
void v5(int x) { unsigned u = 0xFFFFFFFFu; if (x != u) return; if (x == -1) use(5); }
void v6(int x) { unsigned u = 0xFFFFFFFFu; if (x == u) { use(x + 1); } }
int v7(int x) { unsigned u = 0xFFFFFFFFu; if (x == u) { return 10 / (x + 1); } return 0; }Command: cppcheck -q --enable=style,warning --platform=unix64 --template='{line}:{id}:{message}' v.c
Base output (correct):
2..6:knownConditionTrueFalse:Condition 'x==-1' is always true
8:zerodivcond:Either the condition 'x==u' is redundant or there is division by zero at line 8.
PR output:
2:oppositeInnerCondition:Opposite inner 'if' condition leads to a dead code block.
3:oppositeInnerCondition:Opposite inner 'if' condition leads to a dead code block.
4:oppositeInnerCondition:Opposite inner 'if' condition leads to a dead code block.
5:oppositeInnerCondition:Opposite inner 'if' condition leads to a dead code block.
6:knownConditionTrueFalse:Condition 'x==-1' is always false
7:integerOverflowCond:Either the condition 'x==u' is redundant or there is signed integer overflow for expression 'x+1'.
8:integerOverflowCond:Either the condition 'x==u' is redundant or there is signed integer overflow for expression 'x+1'.
--debug for v1:
- PR:
u always 4294967295,x always {4294967295,symbolic=(u)} - base:
x always {-1,symbolic=(u)}
Not changed by the PR: base and the PR give the same wrong results for ordering comparisons of a signed variable with 0x80000000u. Examples: if (x >= 0x80000000u) return; if (x < 0) is reported as always true, and if (x < 0x80000000u) { if (x < 0) } is reported as always true.
Automated review of d117ac7 by Claude. The pull request was built and tested locally.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
New false positives in the mixed-sign fix (ValueFlow::castConstantToOperandType, lib/vf_common.cpp:144-146). The function truncates an unsigned constant to the size of the signed operand. But the comparison happens in the common type, which is at least int and at least as wide as the constant. When the operand is narrower than that type (short/signed char compared with 0xFFFFu/0x8000u/0xFFu, or int with 0xFFFFFFFFul on LP64), the operand is sign-extended first. So 0xFFFFu is not -1 for a short, and no short value equals it. The PR maps it to -1 anyway, then derives facts for that -1 in the condition handler, so reachable conditions are reported as always false or always true. Base gives none of these warnings. I confirmed this by running compiled code: for short s = -1, s == 0xFFFFu is false.
C116's ~x != 1 case (m2) is still reported as always true, as C118 already says. The earlier reproducers from C118/C119 are fixed now. Apart from the new bug above, I found nothing new.
Earlier comments
68 earlier comments, 1 open
@jfdeverge, please fix, resolve or reply to the open comments: #65.
Testing
Build: make -j8 cppcheck testrunner in pr/ and make -j8 cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestStl, TestType, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0.
CLI tests: pytest other_test.py -k "15000 or floating or unsigned_loop or bounded or cast or missing_include" gives 8 passed.
New false positives: narrow signed operand compared with a wider unsigned constant
x.c:
void use(int);
void b1(short s) { if (s == 0xFFFFu) return; if (s == -1) use(1); }
void b2(int x) { if (x == 0xFFFFFFFFul) return; if (x == -1) use(2); }
void b3(signed char c) { if (c != 0xFFu) { if (c == -1) use(3); } }
void b4(short s) { if (s == 0xFFFFu) {} else { if (s == -1) use(4); } }Command: cppcheck -q --enable=style,warning --platform=unix64 --template='{line}:{id}:{message}' x.c
- Base: only the correct
compareValueOutOfTypeRangeErrorwarnings (s == 0xFFFFuis always false, and so on). - PR: the same warnings, plus these:
2:knownConditionTrueFalse:Condition 's==-1' is always false
3:knownConditionTrueFalse:Condition 'x==-1' is always false
4:knownConditionTrueFalse:Condition 'c==-1' is always false
5:knownConditionTrueFalse:Condition 's==-1' is always false
All four conditions are reachable with the value -1 and are then true. A gcc check of short s=-1; s==0xFFFFu, signed char c=-1; c==0xFFu and int x=-1; x==0xFFFFFFFFul prints 0 0 0.
Relational form (y.c):
void use(int);
void c1(short s) { if (s >= 0x8000u) return; if (s < 0) use(1); if (s == 5) use(11); }
void c2(short s) { if (s < 0x8000u) { if (s == 5) use(2); } }- PR:
2: Condition 's<0' is always true2: Condition 's==5' is always false3: oppositeInnerCondition
- Correct behaviour: after the guard,
sis in [0, 32767], sos<0is always false ands==5is possible. - Base: gives only
compareValueOutOfTypeRangeErrorhere.
--debug shows that the constant is turned into -1 or -32768 for the short operand.
Earlier findings re-checked with the PR:
- These are fixed:
- the C118 reproducers (
x == u,x == M,(unsigned)-1,~0u,x != u, the10 / (x + 1)zerodivcond); - the literal cases r1 and m9;
- the resize guard;
a[i + 1]with an unsigned loop index;char == 0xFF;~(uint32_t)x;- the flag enum;
- the
&&loop; - the clamp-then-narrow
&&chain; - the float uninitvar case;
y -= 1after a cast;- a negative value cast to unsigned.
- the C118 reproducers (
- C116's
if (x < 1u || x >= 4294967295u) return; if (~x != 1)is still reported asCondition '~x!=1' is always true.
Automated review of fa04208 by Claude. The pull request was built and tested locally.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found one new problem. It is a false negative that base does not have: a for loop whose start value has only an excluded lower bound no longer gets its negativeIndex warning. For example, void n1(unsigned char n) { for (int i = n - 1; i < 10; i++) a[i] = 0; } gives a[-1] when n == 0. Base reports negativeIndex (index -1), and the PR reports nothing. The cause is the new rule in valueFlowForLoop that an impossible bound "does not witness its endpoint". Here the endpoint can be reached, because n can be 0. If this is intended, a Trac ticket would cover it, as danmar suggested for false negatives.
The latest reported false positive (C120/C121: short/signed char compared with a wider unsigned constant) is fixed in this version. C116's ~x != 1 case is still reported as always true. I found no other new problems.
Earlier comments
70 earlier comments, 1 open
@jfdeverge, please fix, resolve or reply to the open comments: #65.
Testing
Build: make -j8 cppcheck testrunner in pr/ and make -j8 cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestStl, TestType, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0.
CLI tests: pytest other_test.py -k "15000 or floating or unsigned_loop or bounded or cast or missing_include" gives 8 passed.
Base vs PR on real files: I ran both with --enable=style,warning,portability --platform=unix64 --library=posix on test/cfg/std.c, std.cpp, posix.c, lib/mathlib.cpp and lib/token.cpp. The only difference is that compareValueOutOfTypeRangeError is no longer reported for bool == 123 in std.cpp. That is intended: compareBoolExpressionWithInt already covers it, and the PR removes the matching suppressions.
New false negative: negativeIndex for a loop start value with only an excluded lower bound
l8.c:
int a[10];
void n1(unsigned char n) { for (int i = n - 1; i < 10; i++) a[i] = 0; }
void n2(unsigned short n) { for (int i = n - 2; i < 10; i++) a[i] = 0; }
void n3(unsigned char n) { for (int i = (int)n - 1; i < 10; i++) a[i] = 0; }
void l7(unsigned char u) { for (int i = u - 300; i < 10; i++) a[i] = 0; }Command: cppcheck -q --enable=style,warning --platform=unix64 --template='{line}:{id}:{message}' l8.c
Base:
2:negativeIndex:Array 'a[10]' accessed at index -1, which is out of bounds.
3:negativeIndex:Array 'a[10]' accessed at index -2, which is out of bounds.
4:negativeIndex:Array 'a[10]' accessed at index -1, which is out of bounds.
5:negativeIndex:Array 'a[10]' accessed at index -300, which is out of bounds.
PR: no output.
All of these indexes really occur (n == 0, u == 0). In --debug both versions show n - 1 with only !<=-2. The PR's for-loop code only forwards an impossible upper bound, or a possible or known witness, so this start value is dropped. if (x < -5) return; for (int i = x; …) is still reported, through the condition.
Re-checked earlier findings with the PR:
- These are all correct now:
- C120/C121 (
short s == 0xFFFFu,x == 0xFFFFFFFFul,c != 0xFFu,s >= 0x8000u); - C37
(uint32_t)x - 1u == 0xFFFFFFFFu; - the resize guard;
i-- > -3;s + 1u == 0u;a[i + 1]with an unsigned index;~(uint32_t)x;wchar_ton win64;- the flag enum;
- the
||loop zerodiv; char == 0xFF;- clamp-then-narrow;
- the float uninitvar case;
(char)0zerodiv;- the
&&loop; - the
x == uvariable constant.
- C120/C121 (
- Still wrong: C116 m2,
void m2(int x) { if (x < 1u || x >= 4294967295u) return; if (~x != 1) use(3); }, is reported asCondition '~x!=1' is always true. - Wrong in base and the PR alike, so not caused by the PR:
const unsigned short M = 0xFFFF; if (s == M) return; if (s == -1)givess==-1always false.if (x > 0x7FFFFFFFu) { if (x >= 0) }givesx>=0always true.
The #15000 reproducer gives Condition '(((sint64)((uint32)tmp))-1LL)>4294967295LL' is always false.
Comments
lib/valueflow.cpp:1945
This drops negativeIndex warnings that base gives and that are real. When the start value of the loop only has an impossible lower bound, that bound now goes forward only as a constraint. Often the endpoint can be reached:
int a[10];
void n1(unsigned char n) { for (int i = n - 1; i < 10; i++) a[i] = 0; } // a[-1] when n == 0
void n2(unsigned short n) { for (int i = n - 2; i < 10; i++) a[i] = 0; } // a[-2]Base reports negativeIndex at index -1 and -2. The PR reports nothing (--platform=unix64). The !<=-2 here comes from n >= 0 on an unsigned operand. The minimum is reached when n == 0. Could a lower bound that comes from the type's own range (unsigned operand, or a cast) be treated as a witness? If this is intended, please open a Trac ticket and add a TODO test.
Automated review of a89fcd0 by Claude. The pull request was built and tested locally.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems in this version.
- Still open: the false positive first reported in C116 is still there. In
void m2(int x) { if (x < 1u || x >= 4294967295u) return; if (~x != 1) use(3); }the PR reportsCondition '~x!=1' is always true. That is wrong: forx == -2,~x == 1. Base gives a different wrong result on this function (x>=4294967295ualways true). - Fixed: the C122
negativeIndexfalse negative (for (int i = n - 1; ...)with an unsignedn). The "tight exclusion" witness invalueFlowForLoopbrings back all of base's warnings. - Reproducer: the #15000 case is reported as
knownConditionTrueFalse. - No new false positives: none of the extra cast, arithmetic, mixed-sign and for-loop cases I tried gave a new false positive. In several of them the PR removes a wrong result that base gives.
Earlier comments
75 earlier comments, 1 open
@jfdeverge, please fix, resolve or reply to the open comments: #69.
Testing
Build: make -j8 cppcheck testrunner in pr/ and make cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestStl, TestType, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0. I did not run the Python CLI tests this time.
How I compared: every result below compares base and PR using cppcheck -q --enable=style,warning,portability --platform=unix64 --template='{line}:{id}:{message}' <file>.
Re-checked earlier findings
C122 is fixed. Test file l8.c, with these extra lines:
u * 2 - 3u - 1 - 1-(int)uint s = u - 1;used as the start valueif (x < -5) return;
The PR reports every negativeIndex that base reports. It also adds one for for (int i = -(int)u; ...), where base gives none. The reported index there is -2147483647, though the real minimum is -255.
C116 m2 is still open:
void use(int);
void m2(int x) { if (x < 1u || x >= 4294967295u) return; if (~x != 1) use(3); }- Base:
2:knownConditionTrueFalse:Condition 'x>=4294967295u' is always true. This is wrong too. - PR:
2:knownConditionTrueFalse:Condition '~x!=1' is always true. This is wrong, becausex == -2passes the guard and gives~x == 1.
The #15000 reproducer, written with stdint.h types, gives Condition '(((int64_t)((uint32_t)tmp))-1LL)>4294967295LL' is always false.
New probes (p1.c, p2.c, p3.c)
p1.c, about 25 cases: casts combined with + - * / % << >> &, and == against unsigned constants. There are no new false positives. There is one new correct result: uint8_t c = (uint8_t)x; c + 1 > 256 is always false.
p2.c: mixed-sign ==/< against unsigned constants, oppositeInnerCondition, and for-loop start values. Results:
- The results match base except for three improvements:
x <= 0xFFFFFFFFuis now correctly reported as always true.- Base's wrong result for
signed char c == 0xFFFFFFFFuis gone. - Base's false
negativeIndexforfor (int i = n - 1; i < 10 && n; i++)is gone.
- Two wrong results are the same in base and the PR, so the PR did not cause them:
if (x >= 0x80000000u) { if (x == 0x80000000u) }givesoppositeInnerCondition.x >= 0xFFFFFFF0u && x < 5givesincorrectLogicOperator.
p3.c: the at-minimum and at-maximum paths of the rewritten checkCompareValueOutOfTypeRange (INT_MIN <= x, s < 0xFFFFFFFFu, 0xFFFFFFFFu > s, s < 0x80000000u, bool, unsigned char and short). The PR removes four wrong "always true" results from base: lines 7, 11 and 22, all short against an unsigned constant. It gives no new false positives.
Real code: I ran both versions on lib/checkcondition.cpp, lib/mathlib.cpp and cli/cmdlineparser.cpp. The warnings were identical. The PR took 394 s and base 346 s, a single run each, so I could not tell whether the difference is noise.
Automated review of bcd8c72 by Claude. The pull request was built and tested locally.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems in this version.
- Still open (already in comments.md, C116):
void m2(int x) { if (x < 1u || x >= 4294967295u) return; if (~x != 1) use(3); }is still reported asCondition '~x!=1' is always true. That is wrong, becausex == -2passes the guard and gives~x == 1. I did not report it again. - Earlier findings: every other finding from the earlier reviews (C37–C122) that I re-ran now gives the correct result.
- New probes: I tried casts, arithmetic, out-of-range comparisons, mixed-sign comparisons, for loops, per-call-path inference, float bounds and C++ casts. They gave no new false positives, false negatives or crashes. In several cases the PR removes a wrong base result, for example:
short s; s > 0xFFFFuwas reported as always false;signed char c; c > 200uwas reported as always false;uint64_t x; x == -1was reported as always false.
- Real code: base and PR gave identical warnings, and the run time was the same.
Earlier comments
75 earlier comments, 1 open
@jfdeverge, please fix, resolve or reply to the open comments: #69.
Testing
Build: make -j8 cppcheck testrunner in pr/ and make -j8 cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestStl, TestType, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0.
CLI tests: pytest other_test.py -k "15000 or floating or unsigned_loop or bounded or cast or missing_include" gives 8 passed.
Earlier reproducers (old.c, w.c, s.cpp, bf.c)
- I re-ran about 30 reproducers from C37–C122 on unix64, plus the
wchar_tcase on win64. - All of them now give correct results, with one exception: C116's
m2still gives5:knownConditionTrueFalse:Condition '~x!=1' is always true. - The bitfield dump check gives
bits="0"for~and++. - The #11592 expectation is restored.
New probes: each one compares base and PR with cppcheck -q --enable=style,warning --platform=unix64 --template='{line}:{id}:{message}'.
a.c, about 30 cast and arithmetic cases:- Covered: shifts,
/ -1,<<=,*=,+=with wrap, unary minus on casts,_Boolcasts, bitfields and 64-bit wrap. - No new false positives.
- The PR removes base's wrong results for
uint64_t u = x; u + 1 == 0,unsigned u = -(uint8_t)x; u > 255,uint8_t y += 200; y < 100andy += 0xFFFFFF00u; y < 10.
- Covered: shifts,
b.c, plaincharwith--funsigned-char,--fsigned-charand--platform=unspecified: the new results are correct for an unsigned char ((char)x == -1is always false), and there is no output with the unspecified platform.c.c, per-call-path inference: functions called with several argument pairs, and values from conditions. There are two correct new warnings:- negativeIndex in
a[x - y]for the callf1(1,2); - zerodiv in
100 / (x - y)for the callf4(5,5). - I found no false "always" conditions.
- negativeIndex in
d.c, float>=/<=guards followed by other comparisons,sqrtand int casts: same results as base.e.c, about 32compareValueOutOfTypeRangecases on unix64, win64 and unix32: every result the PR gives is correct. The PR removes four wrong base results (short/signed charagainst unsigned constants) and two wrong base results foruint64_t/unsigned longagainst -1.f.c, for loops with cast start values or limits: same results as base.g.c, mixed-sign comparisons, includinglong longagainst0xFFFFFFFFu,shortagainst0xFFFFFFFFuandintagainst0xFFFF…ull:short s; if (s == 0xFFFFFFFFu) { if (s == -1) }is now correctly reported as always true; base gave a wrongoppositeInnerCondition.int x; if (0xFFFFFFFFu == x) { if (x == -1) }is now correctly reported as always true; base said always false.
h.cpp, C++ casts:static_castto anenum class, templates, functional casts,constexprand'\xff'. The only new result isunsigned char c; c == '\xff'always false, which is correct.
Real code: I ran both builds with -j3 --enable=style,warning,portability --platform=unix64 --library=posix on lib/checkother.cpp, lib/checkstl.cpp, cli/cmdlineparser.cpp, test/cfg/windows.cpp and test/cfg/gnu.c. The output is identical. The PR took 452 s and base 450 s.
Seen but not caused by the PR: size_t n; std::vector<int> v; if (v.size() < n + 1) is reported as always true by both base and the PR. The PR removes the plain-variable versions of this wrong result (0 < n + 1, n + 1u != 0u).
Automated review of 98a4f1b by Claude. The pull request was built and tested locally.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. The PR now handles correctly the last open item from earlier reviews (C116, ~x != 1 after if (x < 1u || x >= 4294967295u) return;). It no longer reports that condition. Base reported x>=4294967295u as always true there, which was also wrong.
On my probes every result that differs from base is either a correct new warning or a removed wrong base warning. The probes covered casts with arithmetic and compound assignment, loops with cast start values, clamp-and-narrow, mixed-sign comparisons (including constants of promoted rank such as (unsigned short)0xFFFF against short), enums, bitfields, char16_t/char32_t and C++ casts. Examples:
(uint16_t)wrap ony + 1: no longer wrongly always false.(unsigned)x/(uint64_t)xof a value that may be negative: no longer wrongly always false.x < 0x80000000ufollowed byx < 0: base said always true (wrong); the PR says always false (correct).
One wrong result is not caused by the PR: if (x > 5u) { if (x < 0) ... } with int x gives a wrong oppositeInnerCondition in both base and the PR, even though the PR changed isOppositeCond for mixed-sign constants.
Earlier comments
71 earlier comments, 0 open
Testing
Build: make -j8 cppcheck testrunner in pr/ and make -j8 cppcheck in base/ both built without errors.
Unit tests: TestCondition, TestValueFlow, TestBufferOverrun, TestStl, TestType, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all give Tests failed: 0.
CLI tests: pytest other_test.py -k "15000 or floating or unsigned or bounded or cast" gives 4 passed. These include the two new tests.
Probes: each one compares the PR and base with cppcheck -q --enable=style,warning --platform=unix64 --template='{line}:{column}:{id}:{message}'.
m2.c, the PR reproducer and C116'sm2:- The PR reports the reproducer's condition as always false.
m2is no longer reported.- Base gives a wrong
x>=4294967295ualways true.
p.c,short/signed charagainstunsigned short/unsigned charconstants (variables, casts,||, nestedif): the newcompareValueOutOfTypeRangeErrorresults are all correct, because both operands are promoted toint. TheknownConditionTrueFalseresults are the same as base.q.c, 22 cases of a value from(uint8_t)/(int8_t)/(uint16_t)followed by*=,+=,<<=,|=,~,%,/,>>, unary minus and modification through a pointer or function:- The only new result is
y = ~y; y > 255always true, which is correct. - Base's wrong
uint16_t y = y + 1; y == 0always false is gone. - Base's wrong
integerOverflowCondonint8y * yis gone.
- The only new result is
r.c, for loops with cast start values and limits, andINT_MIN/LLONG_MINlimits:- Results are the same as base, except that base's wrong
compareValueOutOfTypeRangeErrorfori < -2147483647-1is replaced by a correctknownConditionTrueFalse. - No crash.
- Results are the same as base, except that base's wrong
s.cpp, enum class, bitfields,char16_t/char32_t,static_cast, functional casts, multiplication of casts,/ -1,<<: the only new result is(uint32_t)x + 0ULL > 4294967295ULLalways false, which is correct.t.c, condition-bounded values followed by casts or wrapping arithmetic: the PR removes 7 wrong base results, for exampleif (x < -5 || x > 5) return; unsigned y = (unsigned)x; y > 5always false, andif (x > 300) { uint8_t y = (uint8_t)x; y == 0 }always false. It adds none.u.c, mixed-sign nested and else-if conditions:x < 0x80000000u→x < 0: correctly always false; base said always true.x <= 5u→x < 0: correctly always false.x > 5u→x < 0: a wrongoppositeInnerConditionin both base and the PR, so not caused by the PR.
Real code: I ran lib/token.cpp with --enable=style,warning. The output is identical and both take 79 s.
Automated review of a66da09 by Claude. The pull request was built and tested locally.
|
Test results for commit a66da09 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-8819/ 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. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
One new false negative. The new for-loop start-value logic in valueFlowForLoop (lib/valueflow.cpp:5798) ignores a start value whose only lower limit is the minimum of its unsigned type. When the loop variable starts from an unsigned variable (for (int i = n; …) with an unsigned char, unsigned short or unsigned n, or a (uint8_t) cast), the PR no longer gives the loop variable a possible value at that minimum. As a result, real negativeIndex errors (a[i - 1]) and zerodiv errors (100 / i) that the merge base reports are lost: n == 0 is a valid input. The code comment treats this lower limit as vacuous, but the start value really can be 0. Earlier reviews did not raise this; C122 only covered i = n - 1.
Apart from this I found no new problems. The unit tests and the PR's CLI tests pass. I re-ran the reproducers of the main earlier findings, and the results are correct now or the same as base.
Comments
72 comments, 0 open, 1 new
@jfdeverge, please fix, resolve or reply to the open and new comments: #72.
For #72 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.
Testing
Build: make -j8 cppcheck testrunner in pr/ and make -j8 cppcheck in base/ both built without errors.
Unit tests: ./testrunner with TestCondition, TestValueFlow, TestBufferOverrun, TestStl, TestType, TestOther, TestFunctions, TestUninitVar and TestSymbolDatabase all exit with 0.
CLI tests: pytest other_test.py -k "15000 or floating or unsigned or bounded or cast or missing_include" gives 8 passed. That includes the two new tests.
New false negative: for-loop start value from an unsigned variable
lp2.c:
#include <stdint.h>
int a[10];
void p1(unsigned char n) { for (int i = n; i < 10; i++) a[i - 1] = 0; }
void p2(unsigned n) { for (unsigned i = n; i < 10; i++) a[i - 1] = 0; }
void p3(int n) { if (n < 0) return; for (int i = n; i < 10; i++) a[i - 1] = 0; }
void p4(void) { for (int i = 0; i < 10; i++) a[i - 1] = 0; }
void p5(int x) { for (int i = (uint8_t)x; i < 10; i++) a[i - 1] = 0; }
void p6(unsigned char n) { for (int i = n; i < 10; i++) a[i - 2] = 0; }
void p7(unsigned short n) { for (int i = n; i < 10; i++) a[i - 1] = 0; }
void p8(unsigned char n) { for (int i = n; i < 10; i++) { int j = i - 1; a[j] = 0; } }Command: cppcheck -q --enable=style,warning --platform=unix64 --template='{line}:{id}:{message}' lp2.c
Base:
3:negativeIndex:Array 'a[10]' accessed at index -1, which is out of bounds.
5:negativeIndex:Array 'a[10]' accessed at index -1, which is out of bounds.
6:negativeIndex:Array 'a[10]' accessed at index -1, which is out of bounds.
7:negativeIndex:Array 'a[10]' accessed at index -1, which is out of bounds.
8:negativeIndex:Array 'a[10]' accessed at index -2, which is out of bounds.
9:negativeIndex:Array 'a[10]' accessed at index -1, which is out of bounds.
10:negativeIndex:Array 'a[10]' accessed at index -1, which is out of bounds.
PR (the same with --check-level=exhaustive):
5:negativeIndex:Either the condition 'n<0' is redundant or the array 'a[10]' is accessed at index -1, which is out of bounds.
6:negativeIndex:Array 'a[10]' accessed at index -1, which is out of bounds.
The warnings for p1, p5, p6, p7 and p8 are lost. Each of them really happens when n == 0 or x == 0.
zerodiv is lost in the same way (lp4.c):
int z1(unsigned char n) { int s = 0; for (int i = n; i < 10; i++) s += 100 / i; return s; }
int z2(unsigned n) { int s = 0; for (unsigned i = n; i < 10; i++) s += 100 / i; return s; }- Base:
1:zerodiv:Division by zero.and2:zerodiv:Division by zero. - PR: no output.
With --debug on p1, base gives i {<=9,!>=10,>=0,!<=-1} and the PR gives i {<=9,!>=10,!<=-1}. The possible >=0 start value is gone.
Other probes (same as base, or better):
- Casts with arithmetic: about 20 cases with
*,<<,/,%, unary minus,++,*=and widening to 64 bits. No new false positives. Two wrong base results are gone:uint8_t d = c + 1; d == 0andc++; c == 0. - Mixed-sign comparisons: 19 cases of
isOppositeCond,||/&&andelse ifwith0xFFFFFFFFu/0x80000000u. Every new result is correct, for examplex > 0x7FFFFFFFu→x >= 0is now always false; base said always true. The remaining wrong results are the same in base. - Shift counts, float
>=/<=guards withlog/sqrt/acos/int casts, enums with an underlying type,boolandcharcasts: same as base. One new correct result:invalidFunctionArgforlog(x)underx <= 0.0 && x >= 0.0. - Earlier reproducers re-checked: resize guard,
a[i + 1]with an unsigned index,char == 0xFF, flag enum,~(uint32_t)x,y -= 1after a cast, negative value cast to unsigned,short == 0xFFFFu,(char)xzerodivcond, andwchar_ton win64. All give correct results. - Timing: two generated files (900 cast/condition statements, and 200 loops with conditions) take the same time with base and the PR.
Automated review of 7e09034 by Claude. The pull request was built and tested locally.
| MathLib::bigint typeMax{}; | ||
| const bool haveRange = initType && ValueFlow::getMinMaxValues(initType, settings.platform, typeMin, typeMax); | ||
| if (initValue == initValueSource->intvalue + 1 && | ||
| (!haveRange || initValueSource->intvalue > typeMin)) { |
There was a problem hiding this comment.
AI review
When the start value's only lower limit is the minimum of its own type, the initValueSource->intvalue > typeMin test drops it. Examples are for (int i = n; …) with unsigned char/unsigned short/unsigned n, or i = (uint8_t)x. The comment calls this limit vacuous, but the minimum can really occur (n == 0). In base, the loop variable gets a possible >=0. The PR gives it nothing, so these real warnings that base reports are lost (--platform=unix64):
int a[10];
void p1(unsigned char n) { for (int i = n; i < 10; i++) a[i - 1] = 0; } // base: negativeIndex -1
void p7(unsigned short n) { for (int i = n; i < 10; i++) a[i - 1] = 0; } // base: negativeIndex -1
void p5(int x) { for (int i = (uint8_t)x; i < 10; i++) a[i - 1] = 0; } // base: negativeIndex -1
int z2(unsigned n) { int s = 0; for (unsigned i = n; i < 10; i++) s += 100 / i; return s; } // base: zerodivWith --debug, base gives i {<=9,!>=10,>=0,!<=-1} and the PR gives i {<=9,!>=10,!<=-1}. The same happens with --check-level=exhaustive. The minimum of an integral type (or of a cast) is a reachable value, so it could be kept as a possible start value. If you keep this behaviour on purpose, please open a Trac ticket and add a TODO test.
There was missing detection of 'MISRA C:2012 Rule 14.3" on some specific code structure generated by Matlab Simulink coder.
We identified these issues by comparing its results with reports previously generated by our company’s internal tool on the same software.
The bug report would have looked like below.
Problem
Cppcheck did not report a MISRA C:2012 Rule 14.3 violation when a comparison
used an integer expression whose range was provably bounded by casts and
constant arithmetic. The existing check handled a variable compared with a
value outside the variable's declared type range, but did not evaluate this
equivalent expression form.
Minimal Reproducer
After the clamp and casts, the left-hand expression cannot exceed
UINT32_MAX - 1. Therefore the final condition is always false and producesthe
knownConditionTrueFalsediagnostic. The company's end-to-end MISRApostprocessor mapping was not exercised here.
Observed Behavior
Before the fix, the final comparison produced no diagnostic. This allowed the
generated-code pattern to pass through the cppcheck-based MISRA pipeline
without the expected Rule 14.3 finding.