This is the mail archive of the gdb-cvs@sourceware.org mailing list for the GDB 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]

[binutils-gdb] Avoid undefined behavior in read_signed_leb128


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=4dd1b46077b2ae7331f274f7888733de5166445c

commit 4dd1b46077b2ae7331f274f7888733de5166445c
Author: Tom Tromey <tom@tromey.com>
Date:   Fri Aug 17 20:19:13 2018 -0600

    Avoid undefined behavior in read_signed_leb128
    
    -fsanitize=undefined pointed out that read_signed_leb128 had an
    undefined left-shift when processing the final byte of a 64-bit leb:
    
        runtime error: left shift of 127 by 63 places cannot be represented in type 'long int'
    
    and an undefined negation:
    
        runtime error: negation of -9223372036854775808 cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself
    
    Both of these problems are readily avoided by havinng
    read_signed_leb128 work in an unsigned type, and then casting to the
    signed type at the return.
    
    gdb/ChangeLog
    2018-10-03  Tom Tromey  <tom@tromey.com>
    
    	* dwarf2read.c (read_signed_leb128): Work in ULONGEST.

Diff:
---
 gdb/ChangeLog    | 4 ++++
 gdb/dwarf2read.c | 6 +++---
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 97ee73b..9731c99 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,9 @@
 2018-10-03  Tom Tromey  <tom@tromey.com>
 
+	* dwarf2read.c (read_signed_leb128): Work in ULONGEST.
+
+2018-10-03  Tom Tromey  <tom@tromey.com>
+
 	* c-exp.y (parse_number): Work in unsigned.  Remove casts.
 
 2018-10-03  Tom Tromey  <tom@tromey.com>
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 4013c19..7004299 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -19627,7 +19627,7 @@ static LONGEST
 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
 		    unsigned int *bytes_read_ptr)
 {
-  LONGEST result;
+  ULONGEST result;
   int shift, num_read;
   unsigned char byte;
 
@@ -19639,7 +19639,7 @@ read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
       byte = bfd_get_8 (abfd, buf);
       buf++;
       num_read++;
-      result |= ((LONGEST) (byte & 127) << shift);
+      result |= ((ULONGEST) (byte & 127) << shift);
       shift += 7;
       if ((byte & 128) == 0)
 	{
@@ -19647,7 +19647,7 @@ read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
 	}
     }
   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
-    result |= -(((LONGEST) 1) << shift);
+    result |= -(((ULONGEST) 1) << shift);
   *bytes_read_ptr = num_read;
   return result;
 }


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