Re: [PATCH] left shift of negative value do_binop src/arith_yacc.c:111:12
Steffen Nurpmeso <[email protected]> Wed, 20 Aug 2025 13:27:53 +0200
| Newsgroups | org.kernel.vger.dash |
|---|---|
| Message-ID | <20250820112753.FJo-4m6j@steffen%sdaoden.eu> |
Harald van Dijk wrote in <[email protected]>: |On 20/08/2025 01:33, Aleksander Ushakov wrote: |> Well, if we take bash with UBSAN instrumentation and try to use a |> similar command with a negative number shift to the left, then at least |> we won't get a sanitizer error. Yes, you can remove the message that |> this shift is invalid, but it is still UB to perform this shift |> according to the C standard. I mean, a check should be added here so as |> not to trigger UBSAN, as it is done in bash. | |I did that in my own version back in 2019, with the exception of subtly |changing behaviour that differed across architectures, and changing |behaviour that resulted in shell crashes: |<https://github.com/hvdijk/gwsh/commit/3b2307005d9ca18f57151ba239c3a0c3c\ |107d681> |and |<https://github.com/hvdijk/gwsh/commit/c4d6f1b1613c228db1e157a2136c53f55\ |b4530c8>. |As those commits show, there are quite a few more cases to consider than |left shifts of negative values. We (i am only a MUA though, since busybox did not take the code, and kept their broken implemenation; will try again at some time) all return -2 for "-1 << 1", at least on this hardware etc etc. You then return 0 from looking at the commitdiff!?! |dash is free to take those patches, or to decide not to: you are right |that these cases are undefined according to the C standard, but that |means that these cases are also undefined in shell scripts, because |shell arithmetic is defined by reference to the C standard: "The |evaluation of arithmetic expressions shall be equivalent to that |described in Section 6.5, Expressions, of the ISO C standard." The |current behaviour of tolerating it by default, but flagging it when |special options are used, is not my preferred behaviour, but is |defensible, so I will wait for dash's maintainer to chime in on his |preference. --End of <[email protected]> --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)