From 2dceaab4062523c9249c1416edef9c84b9de9d0c Mon Sep 17 00:00:00 2001 From: yp9522 Date: Thu, 17 Jul 2025 15:14:32 +0800 Subject: [PATCH] Ofix parseforinof nullptr Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICMZ90 Signed-off-by: yp9522 --- ets2panda/parser/statementParser.cpp | 12 +++++++ .../ets/ParserImpl_ParserForInOf_nullptr.ets | 31 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/ParserImpl_ParserForInOf_nullptr.ets diff --git a/ets2panda/parser/statementParser.cpp b/ets2panda/parser/statementParser.cpp index aa8941392c9..d8db16063b1 100644 --- a/ets2panda/parser/statementParser.cpp +++ b/ets2panda/parser/statementParser.cpp @@ -781,6 +781,13 @@ std::tuple ParserImpl::Par ir::Expression *rightNode = nullptr; if (lexer_->GetToken().IsForInOf()) { + ES2PANDA_ASSERT(initNode != nullptr); + if (!initNode->IsVariableDeclaration()) { + return {forKind, rightNode, updateNode}; + } + if (initNode->AsVariableDeclaration()->Declarators().empty()) { + return {forKind, rightNode, updateNode}; + } const ir::VariableDeclarator *varDecl = initNode->AsVariableDeclaration()->Declarators().front(); if (lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_IN) { @@ -874,6 +881,9 @@ std::tuple std::tuple ParserImpl::ParseForInOf( ir::Expression *leftNode, ExpressionParseFlags exprFlags, bool isAwait) { + if (lexer_ == nullptr || leftNode == nullptr) { + return {ForStatementKind::UPDATE, nullptr, nullptr, nullptr}; + } ir::Expression *updateNode = nullptr; ir::Expression *rightNode = nullptr; if (lexer_->GetToken().IsForInOf()) { @@ -1005,6 +1015,8 @@ bool ParserImpl::GetCanBeForInOf(ir::Expression *leftNode, ir::AstNode *initNode if (lexer_->GetToken().Type() == lexer::TokenType::PUNCTUATOR_SEMI_COLON) { lexer_->NextToken(); canBeForInOf = false; + } else if (!initNode->IsVariableDeclaration()) { + LogError(diagnostic::INVALID_LEFT_HAND_IN_FOR_OF); } else if (initNode->AsVariableDeclaration()->Declarators().size() > 1 && lexer_->GetToken().IsForInOf()) { LogError(diagnostic::INVALID_LEFT_HAND_IN_FOR_OF, {}, initNode->AsVariableDeclaration()->Declarators()[1]->Start()); diff --git a/ets2panda/test/ast/compiler/ets/ParserImpl_ParserForInOf_nullptr.ets b/ets2panda/test/ast/compiler/ets/ParserImpl_ParserForInOf_nullptr.ets new file mode 100644 index 00000000000..26506215d98 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/ParserImpl_ParserForInOf_nullptr.ets @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +{% for + const enum Direction { + __empty = 1, + } + +/* @@? 16:2 Error SyntaxError: Unexpected token '%'. */ +/* @@? 16:4 Error SyntaxError: Unexpected token 'for'. */ +/* @@? 17:3 Error SyntaxError: Expected '(', got 'const'. */ +/* @@? 31:70 Error SyntaxError: Invalid left-hand side in 'for' statement: must have a single binding. */ +/* @@? 31:70 Error SyntaxError: Expected ';', got 'end of stream'. */ +/* @@? 31:70 Error SyntaxError: Unexpected token 'end of stream'. */ +/* @@? 31:70 Error SyntaxError: Unexpected token, expected ';'. */ +/* @@? 31:70 Error SyntaxError: Unexpected token 'end of stream'. */ +/* @@? 31:70 Error SyntaxError: Expected ')', got 'end of stream'. */ +/* @@? 31:70 Error SyntaxError: Unexpected token 'end of stream'. */ +/* @@? 31:70 Error SyntaxError: Expected '}', got 'end of stream'. */ \ No newline at end of file -- Gitee