Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] Re: Inconsistent behavior of ERR_EXIT with conditionals



On Sun, Nov 6, 2022 at 7:50 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> More questionable are the looping constructs.  I can't come up with a
> way to have the loop end in an error state without the whole shell
> ERREXITing before reaching the end of the loop body.

Found the way (and it should have been obvious):

repeat 1; do false && true; done

The statement `false && true` doesn't itself cause an exit, but does
become $? of the loop.

Now the weird bit is that the attached patch DOES NOT cause a slew of
test failures in C03traps.  E.g. if I run this test standalone:

Src/zsh -f =(<<<"(setopt err_exit
  if true; then
    false
  fi
  print OK
  )")

I get no output and $? = 1.  The exact same code in C03traps.ztst
prints OK, which it should not.  The only difference I can see is that
ZTST_execchunk fiddles with localloops, but I've tried doing that too.

There is the question of why ignoring a false status at the end of a
complex command has so far been considered correct for ERR_EXIT,
according to C03.  This is a disagreement with e.g. bash.
diff --git a/Src/exec.c b/Src/exec.c
index c8bcf4ee5..2422dae91 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -451,7 +451,7 @@ execcursh(Estate state, int do_exec)
     cmdpop();
 
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
 
     return lastval;
 }
diff --git a/Src/loop.c b/Src/loop.c
index db5b3e097..be5261369 100644
--- a/Src/loop.c
+++ b/Src/loop.c
@@ -208,7 +208,7 @@ execfor(Estate state, int do_exec)
     loops--;
     simple_pline = old_simple_pline;
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
     return lastval;
 }
 
@@ -336,7 +336,7 @@ execselect(Estate state, UNUSED(int do_exec))
     loops--;
     simple_pline = old_simple_pline;
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
     return lastval;
 }
 
@@ -478,7 +478,7 @@ execwhile(Estate state, UNUSED(int do_exec))
     popheap();
     loops--;
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
     return lastval;
 }
 
@@ -532,7 +532,7 @@ execrepeat(Estate state, UNUSED(int do_exec))
     loops--;
     simple_pline = old_simple_pline;
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
     return lastval;
 }
 
@@ -587,7 +587,7 @@ execif(Estate state, int do_exec)
 	    lastval = 0;
     }
     state->pc = end;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
 
     return lastval;
 }
@@ -701,7 +701,7 @@ execcase(Estate state, int do_exec)
 
     if (!anypatok)
 	lastval = 0;
-    this_noerrexit = 1;
+    this_noerrexit = (WC_SUBLIST_TYPE(*end) != WC_SUBLIST_END);
 
     return lastval;
 }


Messages sorted by: Reverse Date, Date, Thread, Author