[nvptx] Fix UB in nvptx_assemble_value

When nvptx_assemble_value is called with size == 16, this bitshift runs
into UB:
...
  val &= ((unsigned  HOST_WIDE_INT)2 << (size * BITS_PER_UNIT - 1)) - 1;
...

Fix this by checking the shift amount.

Tested on nvptx.

gcc/ChangeLog:

	* config/nvptx/nvptx.c (nvptx_assemble_value): Fix undefined
	behaviour.
This commit is contained in:
Tom de Vries 2020-09-11 07:13:25 +02:00
parent 60e537a026
commit 5e044c673f

View File

@ -2053,7 +2053,9 @@ nvptx_assemble_value (unsigned HOST_WIDE_INT val, unsigned size)
bool negative_p
= val & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1));
val &= ((unsigned HOST_WIDE_INT)2 << (size * BITS_PER_UNIT - 1)) - 1;
/* Avoid undefined behaviour. */
if (size * BITS_PER_UNIT < HOST_BITS_PER_WIDE_INT)
val &= (HOST_WIDE_INT_1U << (size * BITS_PER_UNIT)) - 1;
for (unsigned part = 0; size; size -= part)
{