This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]