From e036f65ac0df91b03dc6bcda8b043484321c6857 Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 31 Aug 2018 23:17:17 +0200 Subject: [PATCH] Translate-c: Check for error before working on while loop body (#1445) --- src/translate_c.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/translate_c.cpp b/src/translate_c.cpp index 735a671bcc..12c474cd53 100644 --- a/src/translate_c.cpp +++ b/src/translate_c.cpp @@ -3023,12 +3023,19 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt, trans_unary_operator(c, result_used, scope, (const UnaryOperator *)stmt)); case Stmt::DeclStmtClass: return trans_local_declaration(c, scope, (const DeclStmt *)stmt, out_node, out_child_scope); + case Stmt::DoStmtClass: case Stmt::WhileStmtClass: { - AstNode *while_node = trans_while_loop(c, scope, (const WhileStmt *)stmt); + AstNode *while_node = sc == Stmt::DoStmtClass + ? trans_do_loop(c, scope, (const DoStmt *)stmt) + : trans_while_loop(c, scope, (const WhileStmt *)stmt); + + if (while_node == nullptr) + return ErrorUnexpected; + assert(while_node->type == NodeTypeWhileExpr); - if (while_node->data.while_expr.body == nullptr) { + if (while_node->data.while_expr.body == nullptr) while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock); - } + return wrap_stmt(out_node, out_child_scope, scope, while_node); } case Stmt::IfStmtClass: @@ -3053,14 +3060,6 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const Stmt *stmt, case Stmt::UnaryExprOrTypeTraitExprClass: return wrap_stmt(out_node, out_child_scope, scope, trans_unary_expr_or_type_trait_expr(c, scope, (const UnaryExprOrTypeTraitExpr *)stmt)); - case Stmt::DoStmtClass: { - AstNode *while_node = trans_do_loop(c, scope, (const DoStmt *)stmt); - assert(while_node->type == NodeTypeWhileExpr); - if (while_node->data.while_expr.body == nullptr) { - while_node->data.while_expr.body = trans_create_node(c, NodeTypeBlock); - } - return wrap_stmt(out_node, out_child_scope, scope, while_node); - } case Stmt::ForStmtClass: { AstNode *node = trans_for_loop(c, scope, (const ForStmt *)stmt); return wrap_stmt(out_node, out_child_scope, scope, node);