Skip to content

Commit 71de675

Browse files
authored
Fix #14914: fuzzing timeout (hang) in Tokenizer::simplifyTypedef() (#8762)
1 parent 0911ee6 commit 71de675

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

lib/tokenize.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,20 @@ namespace {
563563
if (Token::simpleMatch(start, "typename"))
564564
start = start->next();
565565

566+
const auto checkForRecursion = [this]() {
567+
if (Token::Match(mTypedefToken, "typedef %name% %name% ;"))
568+
return;
569+
for (const Token *tok = mTypedefToken; tok != mEndToken; tok = tok->next()) {
570+
if (tok == mNameToken)
571+
continue;
572+
if (tok->str() != mNameToken->str())
573+
continue;
574+
if (Token::Match(tok->previous(), "struct|class|enum|union"))
575+
continue;
576+
throw InternalError(tok, "recursive typedef encountered");
577+
}
578+
};
579+
566580
// TODO handle unnamed structs etc
567581
if (Token::Match(start, "const| enum|struct|union|class %name%| {")) {
568582
const std::pair<const Token*, Token*> rangeBefore(start, Token::findsimplematch(start, "{"));
@@ -585,24 +599,11 @@ namespace {
585599
}
586600
mNameToken = nameTok;
587601
mEndToken = nameTok->next();
602+
checkForRecursion();
588603
return;
589604
}
590605
}
591606

592-
const auto checkForRecursion = [this]() {
593-
if (Token::Match(mTypedefToken, "typedef %name% %name% ;"))
594-
return;
595-
for (const Token *tok = mTypedefToken; tok != mEndToken; tok = tok->next()) {
596-
if (tok == mNameToken)
597-
continue;
598-
if (tok->str() != mNameToken->str())
599-
continue;
600-
if (Token::Match(tok->previous(), "struct|class|enum|union"))
601-
continue;
602-
throw InternalError(tok, "recursive typedef encountered");
603-
}
604-
};
605-
606607
for (Token* type = start; Token::Match(type, "%name%|*|&|&&"); type = type->next()) {
607608
if (type != start && Token::Match(type, "%name% ;") && !type->isStandardType()) {
608609
mRangeType.first = start;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
typedef struct D{itoftor;}tor tor;

test/testsimplifytypedef.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class TestSimplifyTypedef : public TestFixture {
234234
TEST_CASE(simplifyTypedef161);
235235
TEST_CASE(simplifyTypedef162);
236236
TEST_CASE(simplifyTypedef163);
237+
TEST_CASE(simplifyTypedef164);
237238

238239
TEST_CASE(simplifyTypedefFunction1);
239240
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
@@ -3874,6 +3875,11 @@ class TestSimplifyTypedef : public TestFixture {
38743875
ASSERT_THROW_INTERNAL(tok(code), INTERNAL);
38753876
}
38763877

3878+
void simplifyTypedef164() {
3879+
const char code[] = "typedef struct D{x;}y y;";
3880+
ASSERT_THROW_INTERNAL(tok(code), INTERNAL);
3881+
}
3882+
38773883
void simplifyTypedefFunction1() {
38783884
{
38793885
const char code[] = "typedef void (*my_func)();\n"

0 commit comments

Comments
 (0)