From 5e044c673f8e7d473a2207cd3ba32910c4f5fc75 Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Fri, 11 Sep 2020 07:13:25 +0200 Subject: [PATCH] [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. --- gcc/config/nvptx/nvptx.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gcc/config/nvptx/nvptx.c b/gcc/config/nvptx/nvptx.c index 4fca0ed76d9..0c590d8d1f6 100644 --- a/gcc/config/nvptx/nvptx.c +++ b/gcc/config/nvptx/nvptx.c @@ -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) {