Skip to content

Commit de26c9b

Browse files
committed
Fix #14613 Crash in compilePrecedence2() (function called requires)
1 parent 8119a9d commit de26c9b

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

lib/tokenlist.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,10 +1058,12 @@ static void compilePrecedence2(Token *&tok, AST_state& state)
10581058
else
10591059
compileUnaryOp(tok, state, compileExpression);
10601060
tok = tok2->link()->next();
1061-
} else if (Token::simpleMatch(tok->previous(), "requires {")
1062-
|| (Token::simpleMatch(tok->previous(), ")")
1061+
} else if ((Token::simpleMatch(tok->tokAt(-1), "requires {") && tok->tokAt(-1)->isKeyword())
1062+
|| (Token::simpleMatch(tok->tokAt(-1), ")")
10631063
&& tok->linkAt(-1)
1064-
&& Token::simpleMatch(tok->linkAt(-1)->previous(), "requires ("))) {
1064+
&& Token::simpleMatch(tok->linkAt(-1)->tokAt(-1), "requires (") && tok->linkAt(-1)->tokAt(-1)->isKeyword())) {
1065+
if (!tok->link())
1066+
throw InternalError(tok, "Syntax error, token has no link.", InternalError::AST);
10651067
tok->astOperand1(state.op.top());
10661068
state.op.pop();
10671069
state.op.push(tok);

test/testtokenize.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8328,6 +8328,19 @@ class TestTokenizer : public TestFixture {
83288328

83298329
void cppKeywordInCSource() {
83308330
ASSERT_NO_THROW(tokenizeAndStringify("int throw() {}", dinit(TokenizeOptions, $.cpp = false)));
8331+
8332+
ASSERT_NO_THROW(tokenizeAndStringify("void requires(const char*);\n" // #14613
8333+
"void f() { requires(\"abc\"); }\n",
8334+
dinit(TokenizeOptions, $.cpp = false)));
8335+
8336+
ASSERT_NO_THROW(tokenizeAndStringify("void requires(const char*);\n"
8337+
"void f() { requires(\"abc\"); }\n",
8338+
dinit(TokenizeOptions, $.cpp = true, $.cppstd = Standards::CPP17)));
8339+
8340+
ASSERT_THROW_INTERNAL(tokenizeAndStringify("void requires(const char*);\n"
8341+
"void f() { requires(\"abc\"); }\n",
8342+
dinit(TokenizeOptions, $.cpp = true, $.cppstd = Standards::CPP20)),
8343+
AST);
83318344
}
83328345

83338346
void cppcast() {

0 commit comments

Comments
 (0)