Skip to content

Commit

Permalink
Fix #13095 nullptr dereference in simplifyUsing() (#7264)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Jan 30, 2025
1 parent 35f9925 commit cd2e4e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2872,7 +2872,7 @@ static bool scopesMatch(const std::string &scope1, const std::string &scope2, co
return false;
}

static unsigned int tokDistance(const Token* tok1, const Token* tok2) {
static unsigned int tokDistance(const Token* tok1, const Token* tok2) { // TODO: use index()
unsigned int dist = 0;
const Token* tok = tok1;
while (tok != tok2) {
Expand All @@ -2882,6 +2882,12 @@ static unsigned int tokDistance(const Token* tok1, const Token* tok2) {
return dist;
}

static const Token* skipConstVolatileBackwards(const Token* tok) {
while (Token::Match(tok, "const|volatile"))
tok = tok->previous();
return tok;
}

bool Tokenizer::simplifyUsing()
{
if (!isCPP() || mSettings.standards.cpp < Standards::CPP11)
Expand Down Expand Up @@ -3343,7 +3349,7 @@ bool Tokenizer::simplifyUsing()
}

// Is this a "T(...)" expression where T is a pointer type?
if (Token::Match(tok1, "%name% [({]") && !pointers.empty() && !Token::simpleMatch(tok1->tokAt(-1), ".")) {
if (Token::Match(tok1, "%name% [({]") && !pointers.empty() && !Token::simpleMatch(skipConstVolatileBackwards(tok1->tokAt(-1)), ".")) {
tok1->tokAt(1)->str("(");
tok1->linkAt(1)->str(")");
if (tok1->linkAt(1) == tok1->tokAt(2)) { // T() or T{}
Expand Down
8 changes: 8 additions & 0 deletions test/testsimplifyusing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,14 @@ class TestSimplifyUsing : public TestFixture {
TODO_ASSERT_EQUALS("",
"[test.cpp:6]: (debug) auto token with no type.\n"
"", errout_str());

const char code3[] = "using V = int*;\n"
"auto g() -> const volatile V { return {}; }\n";
const char expected3[] = "auto g ( ) . const volatile int * { return { } ; }";
ASSERT_EQUALS(expected3, tok(code3));
TODO_ASSERT_EQUALS("",
"[test.cpp:2]: (debug) auto token with no type.\n"
"", errout_str());
}

void simplifyUsing33() { // #13090
Expand Down

0 comments on commit cd2e4e8

Please sign in to comment.