This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
ubsan: csky: left shift cannot be represented in type 'int'
- From: Alan Modra <amodra at gmail dot com>
- To: binutils at sourceware dot org
- Date: Wed, 11 Dec 2019 11:24:28 +1030
- Subject: ubsan: csky: left shift cannot be represented in type 'int'
In the following buf is an unsigned char array, so elements are
promoted to int before arithmetic operations.
if (dis_info.info->endian == BFD_ENDIAN_BIG)
while (n--)
val |= buf[n] << (n*8);
else
for (i = 0; i < n; i++)
val |= buf[i] << (i*8);
* csky-dis.c (csky_chars_to_number): Rewrite. Avoid signed
overflow when collecting bytes of a number.
diff --git a/opcodes/csky-dis.c b/opcodes/csky-dis.c
index ffdb596495..af830f3651 100644
--- a/opcodes/csky-dis.c
+++ b/opcodes/csky-dis.c
@@ -140,11 +140,11 @@ csky_chars_to_number (unsigned char * buf, int n)
unsigned int val = 0;
if (dis_info.info->endian == BFD_ENDIAN_BIG)
- while (n--)
- val |= buf[n] << (n*8);
- else
for (i = 0; i < n; i++)
- val |= buf[i] << (i*8);
+ val = val << 8 | (buf[i] & 0xff);
+ else
+ for (i = n - 1; i >= 0; i--)
+ val = val << 8 | (buf[i] & 0xff);
return val;
}
--
Alan Modra
Australia Development Lab, IBM