Repository navigation
Fix #15219 FP constVariablePointer (pointer assignment with offset) - #9033
chrchr-github wants to merge 6 commits into
Conversation
|
Test results for commit 77ceb53 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9033/ 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
The FP from the ticket is fixed, and so are the related FPs for writes through p - 1 (*(p - 1) = 0;, int* q = p - 1; *q = 0;). But the PR adds new false negatives. A pointer subtraction that is only read is no longer reported: return *(p - 1);, char c = *(p - 1); and return (p - 1)[0]; all warned before and are silent now, while *(p + 1) still warns. The deref detection at the top of the loop handles + (hasIncDecPlus) but not -, so a deref of p - n now goes to the non-deref branch and marks p as non-const. The new valueType condition also drops warnings when the - has no valueType, for example p - n > 0 where n has an unknown type. Most of the 23 warnings that the PR test runner lists as removed are probably these cases.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestOther passes: 0 failures, 255 tests.
I compared the two builds with cppcheck -q --enable=style --template='{line}: {id} {message}' FILE.
a.cpp
int f1(int* p) { return *(p - 1); }
int f2(int* p) { return *(p + 1); }
void f3(int* p) { *(p - 1) = 0; }
int f4(int* p) { const int* q = p - 1; return *q; }
int f5(int* p) { const int* q = p + 1; return *q; }
long f6(int* p, int* q) { return p - q; }
int f7(int* p, int n) { return p[-n]; }
int g(const int*);
int f8(int* p) { return g(p - 1); }
int f9(int* p) { return g(p + 1); }
int* f10(int* s) { int* q = s - 1; return q; }
int f11(int* p) { int* q = p - 1; return *q; }
void f12(int* p) { int* q = p - 1; *q = 0; }- base warns on
pin lines 1, 2, 4, 6, 7, 9, 10, 11, 12, 13, onqin line 6, onsin line 11, and givesconstVariablePointerforqin line 12. The warnings on lines 3, 11 and 13 are false positives. - pr warns only on lines 2, 6 (
pandq), 7, 9, 10, and givesconstVariablePointerforqin line 12. - So the PR fixes the FPs on lines 3, 11 and 13. Line 1 (
return *(p - 1);) is a new false negative, while line 2 (*(p + 1)) is still reported.
b.cpp
int f1(int* p, unknown_t n) { return *(p - n); }
int f2(int* p, unknown_t n) { int x = p - n > 0; return x; }
int f3(char* p) { char c = *(p - 1); return c; }
int f4(int* p) { return (p - 1)[0]; }- base gives
constParameterPointerforpon all 4 lines. - pr gives no warning at all. These are new false negatives.
Automated review of 77ceb53 by Claude. The pull request was built and tested locally.
| if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;")) | ||
| if (Token::Match(parent, "%oror%|%comp%|&&|?|!|<<|;")) | ||
| continue; | ||
| if (Token::simpleMatch(parent, "-") && parent->valueType() && parent->valueType()->pointer == 0) |
There was a problem hiding this comment.
AI review
This drops warnings when the pointer subtraction is only read. int f(int* p) { return *(p - 1); }, char c = *(p - 1); and return (p - 1)[0]; all gave constParameterPointer before this PR and give nothing now, while *(p + 1) is still reported. The deref detection above (around line 2021) handles + through hasIncDecPlus but not -, so *(p - 1) reaches this branch and marks p as non-const.
Treating - like + there (parent->str() == "+" || parent->str() == "-") should keep these warnings and still fix #15219.
Also, when the - token has no valueType (e.g. p - n > 0 with n of an unknown type), this no longer continues, so that case is a new false negative too.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. The FP from #15219 is fixed: int* q = s - 1; return q; and writes through p - 1 are no longer reported. The main point from the earlier review is also fixed, because *(p - 1) that is only read warns again. Two false negatives from that review are still there. (p - 1)[0] that is only read gets no warning, and neither does p - n > 0 where - has no valueType. The [ case is not specific to -: (p + 1)[0] is not reported on main either. The same holds for const int* q = p - 1;, return p - 1; from a function that returns const int*, and p - 1 == q. The PR now handles - the same way as +, which keeps these existing false negatives.
Earlier comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Read-only deref of pointer subtraction (*(p - 1)) no longer reported | AI review | general | ✅ Fixed |
| 2 | (p - 1)[0] read-only no longer reported | AI review | general | |
| 3 | p - n with no valueType (unknown n) no longer reported | AI review | general | |
| 4 | Deref detection ignores '-', so *(p - 1) reads mark p non-const | AI review | lib/checkother.cpp:2094 |
✅ Fixed |
| 5 | '-' without valueType no longer continues (p - n > 0 with unknown n) | AI review | lib/checkother.cpp:2094 |
@chrchr-github, please fix, resolve or reply to the open comments: #2, #3, #5.
For #2, #3, #5 (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
I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestOther passes with 0 failures.
I compared the two builds with cppcheck -q --enable=style --template='{line}: {id} {message}' on scratch files with about 30 functions that use p - n in different ways.
False positives on main that the PR fixes:
int* q = s - 1; return q;int* q = p - 1; *q = 0;(p - 1)[0] = 1;(*(p - 1))++;int* f(int* p) { return p - 1; }
Cases where both builds give the same result:
- These still warn:
return *(p - 1);,char c = *(p - 1);,g(p - 1)wheregtakesconst int*,*(p - n)with an unknownn, ande - p.
New true positive in the PR:
return (p - 1)->x;
Warnings that main gives and the PR drops:
| Code | Same code with + on main |
|---|---|
return (p - 1)[0]; |
(p + 1)[0] is not reported |
int x = p - n > 0; with unknown n |
— |
const int* q = p - 1; |
not reported |
const int* f(int* p) { return p - 1; } |
not reported |
return p - 1 == q; |
not reported |
Automated review of 5db5d7f by Claude. The pull request was built and tested locally.
|
Related tickets: https://trac.cppcheck.net/ticket/15226 (FP), https://trac.cppcheck.net/ticket/15227 (FN) |
|
Test results for commit 5db5d7f (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9033/ 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. |
|
This pull request has merge conflicts that must be resolved. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems that are worth blocking on. The FP from #15219 is fixed, and so are the related FPs on main: int* q = p - 1; *q = 0;, --*(p - 1);, *(p - 1) ? ... used as a write target, and (*(p - 1)).x = 1;. A read through *(p - 1) warns again.
One small new instance belongs to the category that is already known, where - is now handled the same way as +: return *(p - 1) ? 1 : 0; and return *(p - 1 - 1); warned on main and are silent with the PR. The + versions of both are not reported on main either. The earlier false negatives are still there, and the author linked ticket 15227 for them.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Read-only *(p - 1) no longer reported |
AI review | general | ✅ Fixed |
| 2 | Read-only (p - 1)[0] no longer reported |
AI review | general | 🎫 Ticket #15227 |
| 3 | p - n with no valueType (unknown n) no longer reported |
AI review | general | 🎫 Ticket #15227 |
| 4 | Deref detection ignores -, so reads through *(p - 1) mark p non-const |
AI review | lib/checkother.cpp:2084 |
✅ Fixed |
| 5 | A - without valueType no longer continues |
AI review | lib/checkother.cpp:2084 |
🎫 Ticket #15227 |
| 6 | Read-only *(p - 1) ? 1 : 0 and *(p - 1 - 1) no longer warn (same as + on main) |
AI review | lib/checkother.cpp:2011 |
🆕 New (false negative) |
@chrchr-github, please fix, resolve or reply to the open and new comments: #6.
For #6 (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
I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestOther: 255 tests, 0 failures.
I compared both builds with cppcheck -q --enable=style --template='{line}: {id} {message}'.
struct S { int x; };
int f1(S* p) { return (p - 1)->x; }
void f2(S* p) { (p - 1)->x = 1; }
void f3(int* p) { *(p - 1) += 2; }
int f4(int* p) { return *(p - 1 - 1); }
void f5(int* p) { *(p - 1 - 1) = 0; }
int f6(int* p, int* e) { return *(e - 1) + (int)(e - p); }
void f7(int* p) { int* q; q = p - 1; *q = 0; }
void f8(int* p) { --*(p - 1); }
int f9(int* p) { return *(p - 1) ? 1 : 0; }
void h(int*);
void f10(int* p) { h(p - 1); }
void f11(int** pp) { **(pp - 1) = 0; }
int f12(int** pp) { return **(pp - 1); }
void f13(S* p) { (*(p - 1)).x = 1; }
int f14(const char* b, char* e) { return (int)(e - b); }
int f15(char* e, char* b) { return e - b > 2; }base warns on lines 5, 7 (p, e), 8, 9, 10, 15, 16 and 17 (e, b). The warnings on lines 8, 9 and 15 are FPs.
pr warns on lines 2 (a new true positive), 7 (p, e), 16 and 17. So the PR fixes the FPs on lines 8, 9 and 15, but it no longer warns on lines 5 (*(p - 1 - 1)) and 10 (*(p - 1) ? 1 : 0), which are both read-only.
On main, *(p + 1 + 1) and *(p + 1) ? 1 : 0 are not reported either. So these drops fit the earlier finding that - is now handled like +. if (*(p - 1)), *(p - 1) == 0 and !*(p - 1) still warn with the PR.
Automated review of d241c95 by Claude. The pull request was built and tested locally.
| bool hasIncDecPlus = false; | ||
| if (parent && (parent->isUnaryOp("*") || (((hasIncDecPlus = parent->isIncDecOp()) || (hasIncDecPlus = (parent->str() == "+"))) && | ||
| bool hasIncDecPlusMinus = false; | ||
| if (parent && (parent->isUnaryOp("*") || (((hasIncDecPlusMinus = parent->isIncDecOp()) || (hasIncDecPlusMinus = Token::Match(parent, "[+-]"))) && |
There was a problem hiding this comment.
AI review
Two more read-only cases that warned on main are silent now:
int f4(int* p) { return *(p - 1 - 1); }
int f9(int* p) { return *(p - 1) ? 1 : 0; }Both gave constParameterPointer on main. With the PR they give nothing. Their + versions are not reported on main either, so this is the same existing gap for + that now also applies to -. It may be worth adding them to ticket 15227.
No description provided.