PR26447 UBSAN: expr.c:1936 left shift of negative value

PR 26447
	* expr.c (expr <O_left_shift>): Do an unsigned shift.
This commit is contained in:
Alan Modra
2020-08-26 14:55:39 +09:30
parent aaf9875ef9
commit b2f386b99c
2 changed files with 22 additions and 6 deletions

View File

@ -1,3 +1,10 @@
2020-08-26 Alan Modra <amodra@gmail.com>
PR 26447
* expr.c (expr <O_left_shift>): Do an unsigned shift.
2020-08-25 Alan Modra <amodra@gmail.com>
2020-08-26 David Faust <david.faust@oracle.com>
* config/tc-bpf.c: Add option -mxbpf to select xbpf isa.
@ -9,7 +16,7 @@
2020-08-25 Alan Modra <amodra@gmail.com>
PR26501
PR 26501
* gas/config/tc-tic54x.c (tic54x_undefined_symbol): Properly treat
misc_symbol_hash entries without values.

View File

@ -1933,12 +1933,21 @@ expr (int rankarg, /* Larger # is higher rank. */
case O_multiply: resultP->X_add_number *= v; break;
case O_divide: resultP->X_add_number /= v; break;
case O_modulus: resultP->X_add_number %= v; break;
case O_left_shift: resultP->X_add_number <<= v; break;
case O_left_shift:
/* We always use unsigned shifts. According to the ISO
C standard, left shift of a signed type having a
negative value is undefined behaviour, and right
shift of a signed type having negative value is
implementation defined. Left shift of a signed type
when the result overflows is also undefined
behaviour. So don't trigger ubsan warnings or rely
on characteristics of the compiler. */
resultP->X_add_number
= (valueT) resultP->X_add_number << (valueT) v;
break;
case O_right_shift:
/* We always use unsigned shifts, to avoid relying on
characteristics of the compiler used to compile gas. */
resultP->X_add_number =
(offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
resultP->X_add_number
= (valueT) resultP->X_add_number >> (valueT) v;
break;
case O_bit_inclusive_or: resultP->X_add_number |= v; break;
case O_bit_or_not: resultP->X_add_number |= ~v; break;