Skip to content

Commit 74e6823

Browse files
Fix #14613 Crash in compilePrecedence2() (function called requires) (#8361)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent f684163 commit 74e6823

2 files changed

Lines changed: 11 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8332,6 +8332,12 @@ class TestTokenizer : public TestFixture {
83328332

83338333
void cppKeywordInCSource() {
83348334
ASSERT_NO_THROW(tokenizeAndStringify("int throw() {}", dinit(TokenizeOptions, $.cpp = false)));
8335+
8336+
const char code[] = "void requires(const char*);\n" // #14613
8337+
"void f() { requires(\"abc\"); }\n";
8338+
ASSERT_NO_THROW(tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = false)));
8339+
ASSERT_NO_THROW(tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = true, $.cppstd = Standards::CPP17)));
8340+
ASSERT_THROW_INTERNAL(tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = true, $.cppstd = Standards::CPP20)), AST);
83358341
}
83368342

83378343
void cppcast() {

0 commit comments

Comments
 (0)