Skip to content

Commit a2eaeaf

Browse files
Fix #10253 arrayIndexOutOfBounds issue in inline function lacks context (#9034)
1 parent 95fea78 commit a2eaeaf

5 files changed

Lines changed: 62 additions & 42 deletions

File tree

‎lib/checkbufferoverrun.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static bool getDimensionsEtc(const Token * const arrayToken, const Settings &set
221221
return ChildrenToVisit::op1_and_op2;
222222
});
223223
}
224-
} else if (const Token *stringLiteral = array->getValueTokenMinStrSize(settings, &path)) {
224+
} else if (const Token *stringLiteral = array->getValueTokenMinStrSize(settings, &path, &errorPath)) {
225225
Dimension dim;
226226
dim.tok = nullptr;
227227
dim.num = Token::getStrArraySize(stringLiteral);
@@ -360,7 +360,7 @@ void CheckBufferOverrunImpl::arrayIndex()
360360
const std::vector<ValueFlow::Value>& indexValues =
361361
getOverrunIndexValues(tok, tok->astOperand1(), dimensions, indexTokens, path);
362362
if (!indexValues.empty())
363-
arrayIndexError(tok, dimensions, indexValues);
363+
arrayIndexError(tok, dimensions, indexValues, std::move(errorPath));
364364
}
365365

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

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

443-
reportError(getErrorPath(tok, index, "Array index out of bounds"),
444+
const ErrorPath errorPathTok = getErrorPath(tok, index, "Array index out of bounds");
445+
errorPath.insert(errorPath.end(), errorPathTok.begin(), errorPathTok.end());
446+
447+
reportError(errorPath,
444448
index->errorSeverity() ? Severity::error : Severity::warning,
445449
index->condition ? "arrayIndexOutOfBoundsCond" : "arrayIndexOutOfBounds",
446450
arrayIndexMessage(tok, dimensions, indexes, condition),
@@ -1342,7 +1346,7 @@ void CheckBufferOverrun::runChecks(const Tokenizer &tokenizer, ErrorLogger& erro
13421346
void CheckBufferOverrun::getErrorMessages(ErrorLogger& errorLogger, const Settings &settings) const
13431347
{
13441348
CheckBufferOverrunImpl c(nullptr, settings, errorLogger);
1345-
c.arrayIndexError(nullptr, std::vector<Dimension>(), std::vector<ValueFlow::Value>());
1349+
c.arrayIndexError(nullptr, std::vector<Dimension>(), std::vector<ValueFlow::Value>(), {});
13461350
c.pointerArithmeticError(nullptr, nullptr, nullptr);
13471351
c.negativeIndexError(nullptr, std::vector<Dimension>(), std::vector<ValueFlow::Value>());
13481352
c.arrayIndexThenCheckError(nullptr, "i");

‎lib/checkbufferoverrun.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class CPPCHECKLIB CheckBufferOverrunImpl : public CheckImpl
102102
void arrayIndex();
103103
void arrayIndexError(const Token* tok,
104104
const std::vector<Dimension>& dimensions,
105-
const std::vector<ValueFlow::Value>& indexes);
105+
const std::vector<ValueFlow::Value>& indexes,
106+
ErrorPath errorPath);
106107
void negativeIndexError(const Token* tok,
107108
const std::vector<Dimension>& dimensions,
108109
const std::vector<ValueFlow::Value>& indexes);

‎lib/token.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1978,12 +1978,13 @@ const ValueFlow::Value * Token::getInvalidValue(const Token *ftok, nonneg int ar
19781978
return ret;
19791979
}
19801980

1981-
const Token *Token::getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path) const
1981+
const Token *Token::getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path, ErrorPath* errorPath) const
19821982
{
19831983
if (!mImpl->mValues)
19841984
return nullptr;
19851985
const Token *ret = nullptr;
19861986
int minsize = INT_MAX;
1987+
auto itMin = mImpl->mValues->end();
19871988
for (auto it = mImpl->mValues->begin(); it != mImpl->mValues->end(); ++it) {
19881989
if (it->isTokValue() && it->tokvalue && it->tokvalue->tokType() == Token::eString) {
19891990
const int size = getStrSize(it->tokvalue, settings);
@@ -1992,9 +1993,12 @@ const Token *Token::getValueTokenMinStrSize(const Settings &settings, MathLib::b
19921993
ret = it->tokvalue;
19931994
if (path)
19941995
*path = it->path;
1996+
itMin = it;
19951997
}
19961998
}
19971999
}
2000+
if (errorPath && itMin != mImpl->mValues->end())
2001+
errorPath->insert(errorPath->end(), itMin->errorPath.begin(), itMin->errorPath.end());
19982002
return ret;
19992003
}
20002004

‎lib/token.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ class CPPCHECKLIB Token {
14151415
const ValueFlow::Value* getContainerSizeValue(MathLib::bigint val) const;
14161416

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

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

0 commit comments

Comments
 (0)