From: Alan Modra Date: Fri, 18 Jun 2021 12:57:01 +0000 (+0930) Subject: ubsan errors when 32-bit bfd X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=03e689aaac95da0b22f7689a2c307e8ddd99ae2c;p=binutils-gdb.git ubsan errors when 32-bit bfd A shift count exceeding the size of the value is undefined behaviour, and so is negating a signed LONG_MIN. * config/tc-z80.c (signed_overflow, unsigned_overflow): Avoid UB. --- diff --git a/gas/ChangeLog b/gas/ChangeLog index ff0f65cf00d..e217c8243a4 100644 --- a/gas/ChangeLog +++ b/gas/ChangeLog @@ -1,3 +1,7 @@ +2021-06-19 Alan Modra + + * config/tc-z80.c (signed_overflow, unsigned_overflow): Avoid UB. + 2021-06-19 Alan Modra * testsuite/gas/ppc/raw.s: Use 0 as pli constant. diff --git a/gas/config/tc-z80.c b/gas/config/tc-z80.c index 47d1405c2b6..303296b7dab 100644 --- a/gas/config/tc-z80.c +++ b/gas/config/tc-z80.c @@ -3703,14 +3703,14 @@ md_assemble (char *str) static int signed_overflow (signed long value, unsigned bitsize) { - signed long max = (signed long)(1UL << (bitsize-1)); - return value < -max || value >= max; + signed long max = (signed long) ((1UL << (bitsize - 1)) - 1); + return value < -max - 1 || value > max; } static int unsigned_overflow (unsigned long value, unsigned bitsize) { - return (value >> bitsize) != 0; + return value >> (bitsize - 1) >> 1 != 0; } static int