Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13564 Crash in simplifyTypedefCpp() #7234

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ void Tokenizer::simplifyTypedefCpp()
const bool isPointerTypeCall = !inOperator && Token::Match(tok2, "%name% ( )") && !pointers.empty();

// start substituting at the typedef name by replacing it with the type
const Token * const location = tok2;
const Token* location = tok2;
for (Token* tok3 = typeStart; tok3 && (tok3->str() != ";"); tok3 = tok3->next())
tok3->isSimplifiedTypedef(true);
if (isPointerTypeCall) {
Expand Down Expand Up @@ -2081,6 +2081,7 @@ void Tokenizer::simplifyTypedefCpp()
if (constTok && !functionPtr) {
tok2 = simplifyTypedefInsertToken(tok2, "const", location);
constTok->deleteThis();
location = constTok;
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedef155);
TEST_CASE(simplifyTypedef156);
TEST_CASE(simplifyTypedef157);
TEST_CASE(simplifyTypedef158);

TEST_CASE(simplifyTypedefFunction1);
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
Expand Down Expand Up @@ -3773,6 +3774,16 @@ class TestSimplifyTypedef : public TestFixture {
ASSERT_EQUALS(exp, tok(code));
}

void simplifyTypedef158() {
const char code[] = "void f() {\n"
" typedef const char* const triple[3];\n"
" static const triple data[] = { {\"a\" , \"b\" , \"c\" } };\n"
"}\n";
const char exp[] = "void f ( ) { static const char * const data [ ] [ 3 ] = { { \"a\" , \"b\" , \"c\" } } ; }";
const char cur[] = "void f ( ) { static const char * const const data [ ] [ 3 ] = { { \"a\" , \"b\" , \"c\" } } ; }";
TODO_ASSERT_EQUALS(exp, cur, tok(code));
}

void simplifyTypedefFunction1() {
{
const char code[] = "typedef void (*my_func)();\n"
Expand Down
Loading