gdb: fix undefined behavior reported in copy_bitwise
authorArtur Shepilko <nomadbyte@gmail.com>
Fri, 10 Apr 2020 14:56:43 +0000 (10:56 -0400)
committerSimon Marchi <simon.marchi@polymtl.ca>
Sat, 11 Apr 2020 01:04:03 +0000 (21:04 -0400)
gdb version 9.1, built with clang 8.0.0 on Ubuntu 18.04 (x86_64);
--enable-ubsan (for clang's undefined behavior sanitizer)

Executing command; `maint selftest copy_bitwise` bombs in runtime error:
../../gdb/utils.c:3432:28: runtime error: left shift of negative value -1

Closer look reveals the offending shift: `(~0 << nbits)`, apparently 0
is treated as signed int, resulting in negative complement. Explicitly
stating it unsigned 0U  fixes it and the `copy_bitwise` test passes
ok.

gdb/ChangeLog
gdb/utils.c

index 661a41467bbef9b60671137b5b74983405193d13..81102ee569bcb841d38688b5ac0eb905ba043261 100644 (file)
@@ -1,3 +1,8 @@
+2020-04-10  Artur Shepilko  <nomadbyte@gmail.com>
+
+       * utils.c (copy_bitwise): Use unsigned 0 constant as operand of
+       bit shift.
+
 2020-04-10  Tom Tromey  <tromey@adacore.com>
 
        * symfile.c (symbol_file_add_separate): Preserve OBJF_MAINLINE.
index bda6bbf5b0e777e859c94dc23c68bb5bdb9024e1..f5b20331b1e2f2966cea7abce27836b639874508 100644 (file)
@@ -3433,7 +3433,7 @@ copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
        buf |= *source << avail;
 
       buf &= (1 << nbits) - 1;
-      *dest = (*dest & (~0 << nbits)) | buf;
+      *dest = (*dest & (~0U << nbits)) | buf;
     }
 }
 
This page took 0.030571 seconds and 4 git commands to generate.