From 1fcea5fb03ffe701c8eac1575cac64afd49f9e8f Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 7 Feb 2023 22:36:42 +0100 Subject: [PATCH] Don't crash on C++23 code (#4771) --- lib/tokenlist.cpp | 2 +- test/testtokenize.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 87cd9b9dc..0e72ce2c6 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1598,7 +1598,7 @@ static Token * createAstAtToken(Token *tok, bool cpp) semicolon2->astOperand2(state3.op.top()); semicolon1->astOperand2(semicolon2); } else { - if (!cpp || !Token::simpleMatch(state2.op.top(), ":")) + if (!cpp || state2.op.empty() || !Token::simpleMatch(state2.op.top(), ":")) throw InternalError(tok, "syntax error", InternalError::SYNTAX); semicolon1->astOperand2(state2.op.top()); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 01be951e3..edd8a00a7 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -434,6 +434,7 @@ private: TEST_CASE(noCrash4); TEST_CASE(noCrash5); // #10603 TEST_CASE(noCrash6); // #10212 + TEST_CASE(noCrash7); // --check-config TEST_CASE(checkConfiguration); @@ -7357,6 +7358,12 @@ private: "struct c, true> {};\n")); } + void noCrash7() { + ASSERT_THROW(tokenizeAndStringify("void g() {\n"// TODO: don't throw + " for (using T = int; (T)false;) {}\n" // C++23 P2360R0: Extend init-statement to allow alias-declaration + "}\n"), InternalError); + } + void checkConfig(const char code[]) { errout.str("");