Skip to content

Commit 682810d

Browse files
Fix #14613 Crash in compilePrecedence2() (function called requires) (#8371)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent ac49e4e commit 682810d

2 files changed

Lines changed: 13 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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8331,6 +8331,14 @@ class TestTokenizer : public TestFixture {
83318331

83328332
void cppKeywordInCSource() {
83338333
ASSERT_NO_THROW(tokenizeAndStringify("int throw() {}", dinit(TokenizeOptions, $.cpp = false)));
8334+
8335+
const char code[] = "void requires(const char*);\n" // #14613
8336+
"void f() { requires(\"abc\"); }\n";
8337+
ASSERT_NO_THROW(tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = false)));
8338+
const Settings s_cpp17 = settingsBuilder().cpp(Standards::CPP17).build();
8339+
ASSERT_NO_THROW(tokenizeAndStringify(code, s_cpp17, true));
8340+
const Settings s_cpp20 = settingsBuilder().cpp(Standards::CPP20).build();
8341+
ASSERT_THROW_INTERNAL(tokenizeAndStringify(code, s_cpp20, true), AST);
83348342
}
83358343

83368344
void cppcast() {

0 commit comments

Comments
 (0)