Re: [PATCH] left shift of negative value do_binop src/arith_yacc.c:111:12

Steffen Nurpmeso <[email protected]> Thu, 31 Jul 2025 21:07:52 +0200
Newsgroups org.kernel.vger.dash
Message-ID <20250731190752.qd9B7w_u@steffen%sdaoden.eu>
Aleksander Ushakov wrote in
 <[email protected]>:
 ..
 |> SUMMARY: UndefinedBehaviorSanitizer: invalid-shift-base arith_yacc.c:111\
 |> :12
 |> Aborted (core dumped)
 ...
 |> echo -ne '$((0|~0<<(0)))' | ./dash
 |
 |There is a fix of the problem:
 |
 |diff --git a/src/arith_yacc.c b/src/arith_yacc.c
 |index 1a087c3..e23060d 100644
 |--- a/src/arith_yacc.c
 |+++ b/src/arith_yacc.c
 |@@ -108,7 +108,9 @@ static intmax_t do_binop(int op, intmax_t a, intmax_t \
 |b)
 |         case ARITH_SUB:
 |                 return a - b;
 |         case ARITH_LSHIFT:
 |-               return a << b;
 |+               if (a < 0)
 |+                       yyerror("left shift of negative value");

imho -- gah, just cast and do it!?!

 |+               return (intmax_t)a << b;
 |         case ARITH_RSHIFT:
 |                 return a >> b;
 |         case ARITH_LT:
 |
 |Same case is placed in DIV branch (when divide on zero). I think it is 
 |analogue.

Btw for div/mod i would take note on

        /* For /,%, avoid lvh=S64_MIN, rhv=-1:
         * CHANGES, bash 4.3 [ac50fbac377e32b98d2de396f016ea81e8ee9961]:
         *   Fixed a bug that caused floating-point exceptions and
         *   overflow errors for the / and % arithmetic operators when
         *   using INTMAX_MIN and -1. */

Aka i have DIV/MOD:

          if(rval == 0){
                  self->sac_error = a_SHEXP_ARITH_ERR_DIV_BY_ZERO;
                  goto jleave;
          }else if(val != S64_MIN || rval != -1)
                  val /= rval;
...
          if(rval == 0){
                  self->sac_error = a_SHEXP_ARITH_ERR_DIV_BY_ZERO;
                  goto jleave;
          }else if(val == S64_MIN && rval == -1)
                  val = 0;
          else
                  val %= rval;
          break;

 --End of <[email protected]>

Greetings and Ciao from, ..sorry.., Germany,

--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)
|
|During summer's humble, here's David Leonard's grumble
|
|The black bear,          The black bear,
|blithely holds his own   holds himself at leisure
|beating it, up and down  tossing over his ups and downs with pleasure
|
|Farewell, dear collar bear