[PATCH] locale: Fix localedata/sort-test undefined behavior

Paul Eggert eggert@cs.ucla.edu
Wed Nov 3 21:19:41 GMT 2021


On 11/3/21 12:50, Andreas Schwab wrote:
>> -      if (r > 0 || (r == 0 && r1 != 0) || (r == 0 && r2 != 0))
>> +      if ((r1 > 0 && r2 > 0)
> That doesn't look the same.  Shouldn't that be (r1 > 0) == (r2 > 0)?

But that would be true when r1 == 0 && r2 == 0, whereas the original 
expression (if there's no overflow) would make it false.

On 11/3/21 12:40, Adhemerval Zanella via Libc-alpha wrote:

> -      r = r1 * r2;
>  
> -      if (r > 0 || (r == 0 && r1 != 0) || (r == 0 && r2 != 0))
> +      if ((r1 > 0 && r2 > 0)
> +	  || ((r1 == 0 || r2 == 0) && r1 != 0)
> +	  || ((r1 == 0 || r2 == 0) && r2 != 0))

This is both too-complicated and (as Andreas wrote) not quite right. 
Instead, I suggest something like this:

   if (signum (r1) != - signum (r2))

where 'signum' is defined by something like this:

static int signum (int n) { return (0 < n) - (n < 0); }

This is clearer and avoids the overflow bug.


More information about the Libc-alpha mailing list