[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 2/3] shell: $(()): sync (comments, a bit reorder, one small fix)
From:       Steffen Nurpmeso <steffen () sdaoden ! eu>
Date:       2022-08-26 17:05:04
Message-ID: c2ac0098a3607567d15cd3ed515aa4031f291f6c.1661533474.git.steffen () sdaoden ! eu
[Download RAW message or body]

---
 shell/shexp-arith.h | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/shell/shexp-arith.h b/shell/shexp-arith.h
index bc2ca52784..71c848455d 100644
--- a/shell/shexp-arith.h
+++ b/shell/shexp-arith.h
@@ -482,7 +482,13 @@ a_shexp__arith_eval(struct a_shexp_arith_ctx *self,
          if(exp_buf != NIL){
             exp_buf = NIL;
             op = a_SHEXP_ARITH_OP_PAREN_RIGHT;
-            ASSERT(sasp->sas_ops_top > sasp->sas_ops);
+            /* Could fail for "1)" (how could that enter at all?)
+             * ASSERT(sasp->sas_ops_top > sasp->sas_ops);
+             * Can only be a syntax error! */
+            if(sasp->sas_ops_top == sasp->sas_ops){
+               self->sac_error = a_SHEXP_ARITH_ERR_SYNTAX;
+               break;
+            }
             goto jtok_go;
          }
 
@@ -522,6 +528,7 @@ a_shexp__arith_eval(struct a_shexp_arith_ctx *self,
       if(cp != ep){
          for(;;){
             c = cp[-1];
+            /* (For example, hyphen-minus as a sh(1) extension!) */
             if(!a_SHEXP_ISVARC_BADNST(c))
                break;
             if(--cp == ep){
@@ -535,7 +542,7 @@ a_shexp__arith_eval(struct a_shexp_arith_ctx *self,
             uz i;
 
             i = P2UZ(cp - ep);
-            /* (Need to move for !_ARITH_ERROR cases) */
+            /* (move not copy for !_ARITH_ERROR cases (says ISO C?)) */
             su_mem_move(sasp->sas_nums_top->sav_var = varp, ep, i);
             varp += i;
             *varp++ = '\0';
@@ -552,7 +559,7 @@ a_shexp__arith_eval(struct a_shexp_arith_ctx *self,
 
       /* An operator.
        * We turn prefix operators to multiple unary plus/minus if
-       * not attached to a variable name (++10 -> + + 10).
+       * not pre- or post-attached to a variable name (++10 -> + + 10).
        * (We adjust postfix to prefix below) */
       if((ep[0] == '+' || ep[0] == '-') && (ep[1] == ep[0])){
          if(sasp->sas_nums_top == sasp->sas_nums ||
@@ -704,7 +711,7 @@ junapre:
                   }
                   op |= x;
 
-                  /* Resolve as much as possible */
+                  /* Resolve as resolve can, need to assert our condition! */
                   while(lprec > a_SHEXP_ARITH_PREC_COND){
                      if(!a_shexp__arith_op_apply(self))
                         goto jleave;
@@ -713,13 +720,12 @@ junapre:
                      lprec = lop & 0xFF;
                   }
 
+                  /* Evaluate condition assertion */
                   ASSERT(sasp->sas_nums_top > sasp->sas_nums);
                   --sasp->sas_nums_top;
 
                   if(sasp->sas_nums_top->sav_var != NIL){
                      if(!(op & a_SHEXP_ARITH_OP_FLAG_WHITE_MASK) &&
-                        (op & a_SHEXP_ARITH_OP_MASK) !=
-                              a_SHEXP_ARITH_OP_ASSIGN &&
                            !a_shexp__arith_val_eval(self, sasp->sas_nums_top))
                         goto jleave;
                      sasp->sas_nums_top->sav_var = NIL;
@@ -729,7 +735,9 @@ junapre:
                      op |= a_SHEXP_ARITH_OP_FLAG_WHITEOUT;
                   op |= *sasp->sas_ops_top & a_SHEXP_ARITH_OP_FLAG_MASK;
 
-                  /* Delay ternary */
+                  /* Delay ternary: this ? op will last until we can resolve
+                   * the entire condition, it's number stack position is used
+                   * as storage for the actual condition result */
                   a_SHEXP_ARITH_ERROR_TRACK( ++sasp->sas_error_track_top; )
                   ++sasp->sas_ops_top;
                   break;
@@ -760,7 +768,7 @@ junapre:
                   op ^= a_SHEXP_ARITH_OP_FLAG_WHITEOUT;
 
                   /* Resolve innermost condition asap.
-                   * In "1 ? 0 ? 5 : 6 : 3", resolve innermost upon :3 */
+                   * In "1?0?5:6:3", resolve innermost upon :3 */
                   while(lprec > a_SHEXP_ARITH_PREC_PAREN_LEFT &&
                         lprec != a_SHEXP_ARITH_PREC_COND){
                      if(!a_shexp__arith_op_apply(self))
@@ -770,9 +778,9 @@ junapre:
                      lprec = lop & 0xFF;
                   }
 
-                  /* If we now see a COLON, we have to resolve further!
-                   * Code flow restrictions of the Dijkstra algorithm!, which
-                   * fits ternary badly (the way we do): pop as pop can! */
+                  /* If at a COLON we have to resolve further, otherwise syntax
+                   * error would happen for 1?2?3:6:7 (due to how Dijkstra's
+                   * algorithm applies, and our squeezing of ?: constructs) */
                   if(lop == a_SHEXP_ARITH_OP_COND_COLON){
                      delay = FAL0;
                      if(!a_shexp__arith_op_apply_colons(self))
@@ -856,14 +864,11 @@ junapre:
                   ) == a_SHEXP_ARITH_OP_COND);
                a_SHEXP_ARITH_ERROR_TRACK( --sasp->sas_error_track_top; )
                --sasp->sas_ops_top;
-               *sasp->sas_ops_top ^= a_SHEXP_ARITH_OP_FLAG_WHITEOUT;
             }
          }
 
-         if(op == a_SHEXP_ARITH_OP_PAREN_RIGHT){
-            self->sac_error = a_SHEXP_ARITH_ERR_SYNTAX;
-            goto jleave;
-         }
+         /* Should have been catched in *ep==\0,exp_buf!=NIL case */
+         ASSERT(op != a_SHEXP_ARITH_OP_PAREN_RIGHT);
       }
 
       /* Push this operator to the stack and remember it */
@@ -896,7 +901,6 @@ jleave:
 
    a_SHEXP_ARITH_L((" < _arith_eval <%lld> ERR<%d>\n",
       self->sac_rv, self->sac_error));
-
    NYD2_OU;
 }
 
@@ -1081,8 +1085,8 @@ a_shexp__arith_op_apply(struct a_shexp_arith_ctx *self){
           * outer group, because it still exists on number stack) */
          nums_top[-1].sav_val = nums_top[0].sav_val;
          nums_top[-1].sav_var = NIL;
-      }else
-         val = nums_top[-1].sav_val;
+      }
+      DBGX( else val = -1; )
 
       sasp->sas_nums_top = nums_top;
 
-- 
2.37.2


--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic