From d1a662dbeaf0ceddf0e46bd1be78310e73369576 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 25 Nov 2024 13:25:47 +0100 Subject: [PATCH] CPlusPlus: Add missing error check to parseExpressionStatement() Fixes: QTCREATORBUG-32043 Change-Id: Ic4f1716339d1277d0a2d5a237fd6ae0389ed2485 Reviewed-by: hjk --- src/libs/3rdparty/cplusplus/Parser.cpp | 24 ++++++++++++++---------- src/libs/3rdparty/cplusplus/Parser.h | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/libs/3rdparty/cplusplus/Parser.cpp b/src/libs/3rdparty/cplusplus/Parser.cpp index 5b3768a14a8..59de0a097a1 100644 --- a/src/libs/3rdparty/cplusplus/Parser.cpp +++ b/src/libs/3rdparty/cplusplus/Parser.cpp @@ -487,15 +487,17 @@ int Parser::find(int token, int stopAt) return 0; } -void Parser::match(int kind, int *token) +bool Parser::match(int kind, int *token) { - if (LA() == kind) + if (LA() == kind) { *token = consumeToken(); - else { - *token = 0; - error(_tokenIndex, "expected token `%s' got `%s'", - Token::name(kind), tok().spell()); + return true; } + + *token = 0; + error(_tokenIndex, "expected token `%s' got `%s'", + Token::name(kind), tok().spell()); + return false; } bool Parser::parseClassOrNamespaceName(NameAST *&node) @@ -3503,7 +3505,8 @@ bool Parser::parseExpressionStatement(StatementAST *&node) DEBUG_THIS_RULE(); if (LA() == T_SEMICOLON) { ExpressionStatementAST *ast = new (_pool) ExpressionStatementAST; - match(T_SEMICOLON, &ast->semicolon_token); + if (!match(T_SEMICOLON, &ast->semicolon_token)) + return false; node = ast; return true; } @@ -3524,9 +3527,10 @@ bool Parser::parseExpressionStatement(StatementAST *&node) ExpressionStatementAST *ast = new (previousPool) ExpressionStatementAST; if (expression) ast->expression = expression->clone(previousPool); - match(T_SEMICOLON, &ast->semicolon_token); - node = ast; - parsed = true; + if (match(T_SEMICOLON, &ast->semicolon_token)) { + node = ast; + parsed = true; + } } _inExpressionStatement = wasInExpressionStatement; diff --git a/src/libs/3rdparty/cplusplus/Parser.h b/src/libs/3rdparty/cplusplus/Parser.h index 7c19c6d0ad6..93687aece7d 100644 --- a/src/libs/3rdparty/cplusplus/Parser.h +++ b/src/libs/3rdparty/cplusplus/Parser.h @@ -273,7 +273,7 @@ public: const Identifier *className(ClassSpecifierAST *ast) const; const Identifier *identifier(NameAST *name) const; - void match(int kind, int *token); + bool match(int kind, int *token); bool maybeAmbiguousStatement(DeclarationStatementAST *ast, StatementAST *&node); bool maybeForwardOrClassDeclaration(SpecifierListAST *decl_specifier_seq) const;