Re: UBSAN error in lib/sh/random.c:79

2023-01-10 Thread Chet Ramey
On 1/10/23 11:43 AM, Greg Wooledge wrote: On Sat, Jan 07, 2023 at 01:42:20PM -0500, Greg Wooledge wrote: Or should the code do the multiplications with unsigned values, store them in unsigned variables, and then replace the subtraction with some kind of conditional that checks which of the two i

Re: UBSAN error in lib/sh/random.c:79

2023-01-10 Thread Greg Wooledge
On Sat, Jan 07, 2023 at 01:42:20PM -0500, Greg Wooledge wrote: > Or should the code do the multiplications with unsigned > values, store them in unsigned variables, and then replace the subtraction > with some kind of conditional that checks which of the two is greater? Here's a version that does

Re: UBSAN error in lib/sh/random.c:79

2023-01-10 Thread Chet Ramey
On 1/7/23 1:45 PM, Martin Schulte wrote: Hello! Am Sat, 07 Jan 2023 19:08:06 +0100 schrieb Andreas Schwab : On Jan 07 2023, Greg Wooledge wrote: ... I think the original overflow can only happen if the argument of intrand32 is bigger than INT_MAX. Question might be if an overflow does any h

Re: UBSAN error in lib/sh/random.c:79

2023-01-10 Thread Chet Ramey
On 1/6/23 8:37 PM, Sam James wrote: Hi folks, I'm currently testing common Linux userland with UndefinedBehaviorSanitizer (UBSAN, -fsanitize=undefined). With Bash 5.2_p15, I get the following with this script: ``` $ cat /tmp/guess_suffix guess_suffix() { tmpdir="${TMPDIR}"/.ecompress$

Re: UBSAN error in lib/sh/random.c:79

2023-01-07 Thread Steffen Nurpmeso
Andreas Schwab wrote in <871qo6f90g@igel.home>: |On Jan 07 2023, Greg Wooledge wrote: |> The variable l is a modulus, so its largest possible value is 127772. |> If the code simply said "l = ret % 127773;" then it wouldn't even be |> an issue. But the % was rewritten, presumably for effic

Re: UBSAN error in lib/sh/random.c:79

2023-01-07 Thread Andreas Schwab
On Jan 07 2023, Greg Wooledge wrote: > The variable l is a modulus, so its largest possible value is 127772. > If the code simply said "l = ret % 127773;" then it wouldn't even be > an issue. But the % was rewritten, presumably for efficiency. Presumably the assumption was that two divides are m

Re: UBSAN error in lib/sh/random.c:79

2023-01-07 Thread Andreas Schwab
On Jan 07 2023, Martin Schulte wrote: > Hello! > > Am Sat, 07 Jan 2023 19:08:06 +0100 schrieb Andreas Schwab > : > >> On Jan 07 2023, Greg Wooledge wrote: >> ... >> I think the original overflow can only happen if the argument of >> intrand32 is bigger than INT_MAX. > > Question might be if an ov

Re: UBSAN error in lib/sh/random.c:79

2023-01-07 Thread Martin Schulte
Hello! Am Sat, 07 Jan 2023 19:08:06 +0100 schrieb Andreas Schwab : > On Jan 07 2023, Greg Wooledge wrote: > ... > I think the original overflow can only happen if the argument of > intrand32 is bigger than INT_MAX. Question might be if an overflow does any harm - or maybe even is intended... B

Re: UBSAN error in lib/sh/random.c:79

2023-01-07 Thread Greg Wooledge
On Sat, Jan 07, 2023 at 07:08:06PM +0100, Andreas Schwab wrote: > On Jan 07 2023, Greg Wooledge wrote: > > > I think this patch might be correct: > > > > > > --- lib/sh/random.c.orig2023-01-07 12:26:09.049950519 -0500 > > +++ lib/sh/random.c 2023-01-07 12:26:27.469974730 -0500 > > @@ -70,8 +70

Re: UBSAN error in lib/sh/random.c:79

2023-01-07 Thread Andreas Schwab
On Jan 07 2023, Greg Wooledge wrote: > I think this patch might be correct: > > > --- lib/sh/random.c.orig 2023-01-07 12:26:09.049950519 -0500 > +++ lib/sh/random.c 2023-01-07 12:26:27.469974730 -0500 > @@ -70,8 +70,8 @@ > There are lots of other combinations of constants to use; look

Re: UBSAN error in lib/sh/random.c:79

2023-01-07 Thread Greg Wooledge
On Fri, Jan 06, 2023 at 09:00:30PM -0500, Greg Wooledge wrote: > On Sat, Jan 07, 2023 at 01:37:30AM +, Sam James wrote: > > random.c:79:21: runtime error: signed integer overflow: 31789 * 127773 > > cannot be represented in type 'int' > > #0 0x559791a301ce in intrand32 > > /usr/src/debug/

Re: UBSAN error in lib/sh/random.c:79

2023-01-06 Thread Greg Wooledge
On Sat, Jan 07, 2023 at 01:37:30AM +, Sam James wrote: > $ cat /tmp/guess_suffix > guess_suffix() { > tmpdir="${TMPDIR}"/.ecompress$$.${RANDOM} > } > guess_suffix > $ export UBSAN_OPTIONS="print_stacktrace=1:halt_on_error=1" > $ bash -x /tmp/guess_suffix > + guess_suffix > random.c:79:

UBSAN error in lib/sh/random.c:79

2023-01-06 Thread Sam James
Hi folks, I'm currently testing common Linux userland with UndefinedBehaviorSanitizer (UBSAN, -fsanitize=undefined). With Bash 5.2_p15, I get the following with this script: ``` $ cat /tmp/guess_suffix guess_suffix() { tmpdir="${TMPDIR}"/.ecompress$$.${RANDOM} } guess_suffix ``` It seem