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

Harald van Dijk <[email protected]> Wed, 20 Aug 2025 03:46:01 +0100
Newsgroups org.kernel.vger.dash
Message-ID <[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/3b2307005d9ca18f57151ba239c3a0c3c107d681> 
and 
<https://github.com/hvdijk/gwsh/commit/c4d6f1b1613c228db1e157a2136c53f55b4530c8>. 
As those commits show, there are quite a few more cases to consider than 
left shifts of negative values.

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.

Cheers,
Harald van Dijk