Skip to content
14 changes: 9 additions & 5 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ static bool getDimensionsEtc(const Token * const arrayToken, const Settings &set
return ChildrenToVisit::op1_and_op2;
});
}
} else if (const Token *stringLiteral = array->getValueTokenMinStrSize(settings, &path)) {
} else if (const Token *stringLiteral = array->getValueTokenMinStrSize(settings, &path, &errorPath)) {
Dimension dim;
dim.tok = nullptr;
dim.num = Token::getStrArraySize(stringLiteral);
Expand Down Expand Up @@ -360,7 +360,7 @@ void CheckBufferOverrunImpl::arrayIndex()
const std::vector<ValueFlow::Value>& indexValues =
getOverrunIndexValues(tok, tok->astOperand1(), dimensions, indexTokens, path);
if (!indexValues.empty())
arrayIndexError(tok, dimensions, indexValues);
arrayIndexError(tok, dimensions, indexValues, std::move(errorPath));
}

// Negative index
Expand Down Expand Up @@ -421,7 +421,8 @@ static std::string arrayIndexMessage(const Token* tok,

void CheckBufferOverrunImpl::arrayIndexError(const Token* tok,
const std::vector<Dimension>& dimensions,
const std::vector<ValueFlow::Value>& indexes)
const std::vector<ValueFlow::Value>& indexes,
ErrorPath errorPath)
{
if (!tok) {
reportError(tok, Severity::error, "arrayIndexOutOfBounds", "Array 'arr[16]' accessed at index 16, which is out of bounds.", CWE_BUFFER_OVERRUN, Certainty::normal);
Expand All @@ -440,7 +441,10 @@ void CheckBufferOverrunImpl::arrayIndexError(const Token* tok,
index = &indexValue;
}

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());

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

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.


reportError(errorPath,
index->errorSeverity() ? Severity::error : Severity::warning,
index->condition ? "arrayIndexOutOfBoundsCond" : "arrayIndexOutOfBounds",
arrayIndexMessage(tok, dimensions, indexes, condition),
Expand Down Expand Up @@ -1342,7 +1346,7 @@ void CheckBufferOverrun::runChecks(const Tokenizer &tokenizer, ErrorLogger& erro
void CheckBufferOverrun::getErrorMessages(ErrorLogger& errorLogger, const Settings &settings) const
{
CheckBufferOverrunImpl c(nullptr, settings, errorLogger);
c.arrayIndexError(nullptr, std::vector<Dimension>(), std::vector<ValueFlow::Value>());
c.arrayIndexError(nullptr, std::vector<Dimension>(), std::vector<ValueFlow::Value>(), {});
c.pointerArithmeticError(nullptr, nullptr, nullptr);
c.negativeIndexError(nullptr, std::vector<Dimension>(), std::vector<ValueFlow::Value>());
c.arrayIndexThenCheckError(nullptr, "i");
Expand Down
3 changes: 2 additions & 1 deletion lib/checkbufferoverrun.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class CPPCHECKLIB CheckBufferOverrunImpl : public CheckImpl
void arrayIndex();
void arrayIndexError(const Token* tok,
const std::vector<Dimension>& dimensions,
const std::vector<ValueFlow::Value>& indexes);
const std::vector<ValueFlow::Value>& indexes,
ErrorPath errorPath);
void negativeIndexError(const Token* tok,
const std::vector<Dimension>& dimensions,
const std::vector<ValueFlow::Value>& indexes);
Expand Down
6 changes: 5 additions & 1 deletion lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1978,12 +1978,13 @@ const ValueFlow::Value * Token::getInvalidValue(const Token *ftok, nonneg int ar
return ret;
}

const Token *Token::getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path) const
const Token *Token::getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path, ErrorPath* errorPath) const
{
if (!mImpl->mValues)
return nullptr;
const Token *ret = nullptr;
int minsize = INT_MAX;
auto itMin = mImpl->mValues->end();
for (auto it = mImpl->mValues->begin(); it != mImpl->mValues->end(); ++it) {
if (it->isTokValue() && it->tokvalue && it->tokvalue->tokType() == Token::eString) {
const int size = getStrSize(it->tokvalue, settings);
Expand All @@ -1992,9 +1993,12 @@ const Token *Token::getValueTokenMinStrSize(const Settings &settings, MathLib::b
ret = it->tokvalue;
if (path)
*path = it->path;
itMin = it;
}
}
}
if (errorPath && itMin != mImpl->mValues->end())
errorPath->insert(errorPath->end(), itMin->errorPath.begin(), itMin->errorPath.end());
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ class CPPCHECKLIB Token {
const ValueFlow::Value* getContainerSizeValue(MathLib::bigint val) const;

const Token *getValueTokenMaxStrLength() const;
const Token *getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path = nullptr) const;
const Token *getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path = nullptr, ErrorPath* errorPath = nullptr) const;

/** Add token value. Return true if value is added. */
bool addValue(const ValueFlow::Value &value);
Expand Down
Loading
Loading