Repository navigation
Fix #10253 arrayIndexOutOfBounds issue in inline function lacks context - #9034
chrchr-github wants to merge 11 commits into
Conversation
|
Test results for commit 0b1fb56 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9034/ 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 extra note can point at the wrong call. getErrorPath() takes the error path of values().front() of the array operand, which is the first tok value. It does not check that this is the value that caused the error. When a function is called with several string literals, the note can name a call whose argument is long enough and is not out of bounds. I reproduced this. The test only covers the case with a single call.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestBufferOverrun passes: 201 tests, 0 failures.
The note points at the wrong call (scratch/a.c):
char g(const char* s) {
return s[6];
}
void f() {
g("abcdefghij");
g("abc");
}cppcheck -q --template='{file}:{line}:{column}: {id}: {message}' --template-location='{file}:{line}:{column}: note: {info}' a.c, with the PR:
a.c:2:13: arrayIndexOutOfBounds: Array 's[4]' accessed at index 6, which is out of bounds.
a.c:5:7: note: Calling function 'g', 1st argument '"abcdefghij"' value is "abcdefghij"
a.c:2:13: note: Array index out of bounds
The message is about s[4], which is "abc" on line 6. The note names the call on line 5, which passes an 11-byte string and is not out of bounds. Base prints no notes here.
Other cases:
- The single-call case from the PR now gets the "Calling function" note, as intended.
- When the index comes from an argument (
g("abc", 6)), the output is the same as base: only the path of the index is shown. - The CTU case with a local
char buf[4]is unchanged.
Automated review of 0b1fb56 by Claude. The pull request was built and tested locally.
| } else if (mSettings.verbose || mSettings.outputFormat == Settings::OutputFormat::xml || !mSettings.templateLocation.empty()) { | ||
| errorPath = value->errorPath; | ||
| // array passed to subfunction | ||
| if (errorPath.empty() && Token::simpleMatch(errtok, "[") && errtok->astOperand1() && !errtok->astOperand1()->values().empty() && errtok->astOperand1()->values().front().isTokValue()) |
There was a problem hiding this comment.
AI review
values().front() is just the first value of the array operand. It is not necessarily the tok value whose size caused the error. With g("abcdefghij"); g("abc"); and return s[6];, the message is about s[4], but the note says "Calling function 'g', 1st argument '"abcdefghij"'". That points at line 5, a call that is not out of bounds.
The value that produced the dimension should be passed from CheckBufferOverrun, for example by building the error path there, so that the note matches the reported size. This generic helper should not guess.
This block is also in CheckImpl::getErrorPath, which every check uses. Any check that reports at a [ with a value that has no error path will now get notes from an unrelated tok value of the array operand.
|
Test results for commit 54fd8e4 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9034/ 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 earlier problem is fixed. getValueTokenMinStrSize() now takes the error path from the smallest string value, which is the value that sets the reported size, and CheckImpl::getErrorPath is no longer changed.
I found one new problem. arrayIndexError() adds the error path of the buffer size in every case, but getErrorPath() adds the index's error path only in verbose mode, with xml output or when --template-location is set. Without those, the location chain of the warning now starts with the line where the buffer was allocated or assigned ([a.c:3] -> [a.c:6]), and the index's path is still left out. That is why so many ASSERTs in testbufferoverrun.cpp changed from one location to two. It is probably also the +1/-1 difference in the daily package test (C4).
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Note may point at a call that is not the one that caused the out-of-bounds size | AI review | lib/checkimpl.cpp:51 |
✅ Fixed |
| 2 | Change in the generic CheckImpl::getErrorPath affects all checks | AI review | lib/checkimpl.cpp:51 |
❔ Code removed |
| 3 | Size error path is added even in non-verbose mode, unlike getErrorPath() | AI review | lib/checkbufferoverrun.cpp:445 |
🆕 New |
@chrchr-github, please fix, resolve or reply to the open and new comments: #3.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestBufferOverrun passes with 0 failures.
Test file scratch/a.c:
#include <stdlib.h>
void f(int c) {
char *p = malloc(10);
int i = 0;
if (c) i = 10;
p[i] = 0;
free(p);
}
char g(const char* s) { return s[6]; }
void h() { g("abcdefghij"); g("abc"); }
void k() {
char *s = "abc";
s[-1] = 0;
}- With
--template-location(and with the default CLI template), the size notes appear: "Assign p, buffer with size 10", and "Calling function 'g', 1st argument '"abc"'". The second note now names the right call, line 10'sg("abc"), so the wrong-call note from C2 is fixed. - With a template that has no location part,
cppcheck -q --template='{callstack}: {id}: {message}' a.c:
base:
[a.c:6]: arrayIndexOutOfBounds: Array 'p[10]' accessed at index 10, which is out of bounds.
[a.c:9]: arrayIndexOutOfBounds: Array 's[4]' accessed at index 6, which is out of bounds.
[a.c:13]: negativeIndex: Array 's[4]' accessed at index -1, which is out of bounds.
PR:
[a.c:3] -> [a.c:6]: arrayIndexOutOfBounds: Array 'p[10]' accessed at index 10, which is out of bounds.
[a.c:10] -> [a.c:9]: arrayIndexOutOfBounds: Array 's[4]' accessed at index 6, which is out of bounds.
[a.c:13]: negativeIndex: Array 's[4]' accessed at index -1, which is out of bounds.
On line 6 the index path (i=10, condition at line 5) is still left out, but the size path (line 3) is now added. negativeIndex is unchanged, so on the same array it reports only one location.
Automated review of 54fd8e4 by Claude. The pull request was built and tested locally.
|
|
||
| reportError(getErrorPath(tok, index, "Array index out of bounds"), | ||
| const ErrorPath errorPathTok = getErrorPath(tok, index, "Array index out of bounds"); | ||
| errorPath.insert(errorPath.end(), errorPathTok.begin(), errorPathTok.end()); |
There was a problem hiding this comment.
AI review
getErrorPath() adds value->errorPath only with verbose, xml output or a non-empty templateLocation. In the other cases it keeps only the condition and the error token. The size's errorPath is added here in every case. With a template that has no location part (--template='{callstack}: {id}: {message}'), the output is now [a.c:3] -> [a.c:6]: ... 'p[10]' accessed at index 10. The size path from line 3 is shown, but the index path (assignment i=10 and the condition at line 5) is still hidden. This is also why many existing testrunner expectations changed from one location to two. Consider adding the size path only under the same condition that getErrorPath uses, or putting the logic in one helper. negativeIndexError does not get the size path either, so the two warnings for the same array are inconsistent.
No description provided.