Skip to content

Commit dc2661d

Browse files
committed
avoid unchecked pointer dereference in simplecpp::TokenList::constFoldQuestionOp()
1 parent e15ea39 commit dc2661d

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

simplecpp.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ void simplecpp::TokenList::constFold()
984984
constFoldComparison(tok);
985985
constFoldBitwise(tok);
986986
constFoldLogicalOp(tok);
987-
constFoldQuestionOp(&tok);
987+
constFoldQuestionOp(tok);
988988

989989
// If there is no '(' we are done with the constant folding
990990
if (tok->op != '(')
@@ -1354,11 +1354,11 @@ void simplecpp::TokenList::constFoldLogicalOp(Token *tok)
13541354
}
13551355
}
13561356

1357-
void simplecpp::TokenList::constFoldQuestionOp(Token **tok1)
1357+
void simplecpp::TokenList::constFoldQuestionOp(Token *&tok1)
13581358
{
13591359
bool gotoTok1 = false;
13601360
// NOLINTNEXTLINE(misc-const-correctness) - technically correct but used to access non-const data
1361-
for (Token *tok = *tok1; tok && tok->op != ')'; tok = gotoTok1 ? *tok1 : tok->next) {
1361+
for (Token *tok = tok1; tok && tok->op != ')'; tok = gotoTok1 ? tok1 : tok->next) {
13621362
gotoTok1 = false;
13631363
if (tok->str() != "?")
13641364
continue;
@@ -1373,8 +1373,8 @@ void simplecpp::TokenList::constFoldQuestionOp(Token **tok1)
13731373
Token * const falseTok = trueTok->next->next;
13741374
if (!falseTok)
13751375
throw std::runtime_error("invalid expression");
1376-
if (condTok == *tok1)
1377-
*tok1 = (condTok->str() != "0" ? trueTok : falseTok);
1376+
if (condTok == tok1)
1377+
tok1 = (condTok->str() != "0" ? trueTok : falseTok);
13781378
deleteToken(condTok->next); // ?
13791379
deleteToken(trueTok->next); // :
13801380
deleteToken(condTok->str() == "0" ? trueTok : falseTok);

simplecpp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ namespace simplecpp {
353353
void constFoldComparison(Token *tok);
354354
void constFoldBitwise(Token *tok);
355355
void constFoldLogicalOp(Token *tok);
356-
void constFoldQuestionOp(Token **tok1);
356+
void constFoldQuestionOp(Token *&tok1);
357357

358358
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
359359
void lineDirective(unsigned int fileIndex, unsigned int line, Location *location);

0 commit comments

Comments
 (0)