From 88336c240496804b3e7796418c1310a9d7803c21 Mon Sep 17 00:00:00 2001
From: Grisha Levit <grishalevit@gmail.com>
Date: Tue, 26 Sep 2023 18:00:04 -0400
Subject: [PATCH] fix up arith for init fail return

loop_level and interrupt_execution don't get restored if the
initialization step in an arithmetic for command has an expansion
failure.

$ for ((j=;;)); do :; done
bash: ((: j=: arithmetic syntax error: operand expected (error token is "=")
$ echo X; break
X
$ echo Y
$
---
 execute_cmd.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

diff --git a/execute_cmd.c b/execute_cmd.c
index 73723db5..9d85bed0 100644
--- a/execute_cmd.c
+++ b/execute_cmd.c
@@ -3156,13 +3156,8 @@ execute_arith_for_command (ARITH_FOR_COM *arith_for_command)
 
   /* Evaluate the initialization expression. */
   expresult = eval_arith_for_expr (arith_for_command->init, &expok);
-  if (expok == 0)
-    {
-      line_number = save_lineno;
-      return (EXECUTION_FAILURE);
-    }
 
-  while (1)
+  while (expok)
     {
       /* Evaluate the test expression. */
       line_number = arith_lineno;
@@ -3170,10 +3165,8 @@ execute_arith_for_command (ARITH_FOR_COM *arith_for_command)
       line_number = save_lineno;
 
       if (expok == 0)
-	{
-	  body_status = EXECUTION_FAILURE;
-	  break;
-	}
+	break;
+
       REAP ();
       if (expresult == 0)
 	break;
@@ -3201,18 +3194,12 @@ execute_arith_for_command (ARITH_FOR_COM *arith_for_command)
       line_number = arith_lineno;
       expresult = eval_arith_for_expr (arith_for_command->step, &expok);
       line_number = save_lineno;
-
-      if (expok == 0)
-	{
-	  body_status = EXECUTION_FAILURE;
-	  break;
-	}
     }
 
   loop_level--; interrupt_execution--;
   line_number = save_lineno;
 
-  return (body_status);
+  return (expok ? body_status : EXECUTION_FAILURE);
 }
 #endif
 
-- 
2.42.0

