Skip to content

Commit cd2e4e8

Browse files
Fix #13095 nullptr dereference in simplifyUsing() (danmar#7264)
1 parent 35f9925 commit cd2e4e8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/tokenize.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,7 +2872,7 @@ static bool scopesMatch(const std::string &scope1, const std::string &scope2, co
28722872
return false;
28732873
}
28742874

2875-
static unsigned int tokDistance(const Token* tok1, const Token* tok2) {
2875+
static unsigned int tokDistance(const Token* tok1, const Token* tok2) { // TODO: use index()
28762876
unsigned int dist = 0;
28772877
const Token* tok = tok1;
28782878
while (tok != tok2) {
@@ -2882,6 +2882,12 @@ static unsigned int tokDistance(const Token* tok1, const Token* tok2) {
28822882
return dist;
28832883
}
28842884

2885+
static const Token* skipConstVolatileBackwards(const Token* tok) {
2886+
while (Token::Match(tok, "const|volatile"))
2887+
tok = tok->previous();
2888+
return tok;
2889+
}
2890+
28852891
bool Tokenizer::simplifyUsing()
28862892
{
28872893
if (!isCPP() || mSettings.standards.cpp < Standards::CPP11)
@@ -3343,7 +3349,7 @@ bool Tokenizer::simplifyUsing()
33433349
}
33443350

33453351
// Is this a "T(...)" expression where T is a pointer type?
3346-
if (Token::Match(tok1, "%name% [({]") && !pointers.empty() && !Token::simpleMatch(tok1->tokAt(-1), ".")) {
3352+
if (Token::Match(tok1, "%name% [({]") && !pointers.empty() && !Token::simpleMatch(skipConstVolatileBackwards(tok1->tokAt(-1)), ".")) {
33473353
tok1->tokAt(1)->str("(");
33483354
tok1->linkAt(1)->str(")");
33493355
if (tok1->linkAt(1) == tok1->tokAt(2)) { // T() or T{}

test/testsimplifyusing.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,14 @@ class TestSimplifyUsing : public TestFixture {
822822
TODO_ASSERT_EQUALS("",
823823
"[test.cpp:6]: (debug) auto token with no type.\n"
824824
"", errout_str());
825+
826+
const char code3[] = "using V = int*;\n"
827+
"auto g() -> const volatile V { return {}; }\n";
828+
const char expected3[] = "auto g ( ) . const volatile int * { return { } ; }";
829+
ASSERT_EQUALS(expected3, tok(code3));
830+
TODO_ASSERT_EQUALS("",
831+
"[test.cpp:2]: (debug) auto token with no type.\n"
832+
"", errout_str());
825833
}
826834

827835
void simplifyUsing33() { // #13090

0 commit comments

Comments
 (0)