Skip to content

Commit 231b47d

Browse files
Fix #14877: heap-use-after-free in Tokenizer::simplifyUsing()
1 parent 86f4c91 commit 231b47d

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

lib/tokenize.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,7 +3015,6 @@ bool Tokenizer::simplifyUsing()
30153015
Token::Match(tok->linkAt(2), "] ] = ::| %name%")))))
30163016
continue;
30173017

3018-
const std::string& name = tok->strAt(1);
30193018
const Token *nameToken = tok->next();
30203019
std::string scope = currentScope->fullName;
30213020
Token *usingStart = tok;
@@ -3064,7 +3063,7 @@ bool Tokenizer::simplifyUsing()
30643063
if (!hasName) {
30653064
std::string newName;
30663065
if (structEnd->strAt(2) == ";")
3067-
newName = name;
3066+
newName = nameToken->str();
30683067
else
30693068
newName = "Unnamed" + std::to_string(mUnnamedCount++);
30703069
TokenList::copyTokens(structEnd->next(), tok, start);
@@ -3211,7 +3210,7 @@ bool Tokenizer::simplifyUsing()
32113210
if (!isTypedefInfoAdded && Token::Match(tok1, "%name% (")) {
32123211
isTypedefInfoAdded = true;
32133212
TypedefInfo usingInfo;
3214-
usingInfo.name = name;
3213+
usingInfo.name = nameToken->str();
32153214
usingInfo.filename = list.file(nameToken);
32163215
usingInfo.lineNumber = nameToken->linenr();
32173216
usingInfo.column = nameToken->column();

test/testsimplifyusing.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class TestSimplifyUsing : public TestFixture {
9898
TEST_CASE(simplifyUsing10335);
9999
TEST_CASE(simplifyUsing10720);
100100
TEST_CASE(simplifyUsing13873); // function declaration
101+
TEST_CASE(simplifyUsing14877);
101102

102103
TEST_CASE(scopeInfo1);
103104
TEST_CASE(scopeInfo2);
@@ -1667,6 +1668,12 @@ class TestSimplifyUsing : public TestFixture {
16671668
ASSERT_EQUALS("namespace NS1 { void * f ( ) ; }", tok(code3));
16681669
}
16691670

1671+
void simplifyUsing14877() {
1672+
const char code[] = "using C = struct C { C() {} };";
1673+
const char expected[] = "struct C { C ( ) { } } ;";
1674+
ASSERT_EQUALS(expected, tok(code));
1675+
}
1676+
16701677
void scopeInfo1() {
16711678
const char code[] = "struct A {\n"
16721679
" enum class Mode { UNKNOWN, ENABLED, NONE, };\n"

0 commit comments

Comments
 (0)