diff mbox

Clear upper bits during sign extension

Message ID 878uhrvumr.fsf@codesourcery.com
State New
Headers show

Commit Message

Yao Qi Dec. 29, 2014, 6:26 a.m. UTC
Doug Evans <dje@google.com> writes:

> It's not immediately clear to this reader that undefined behaviour is
> avoided here.
> E.g., what if sizeof (LONGEST) == 8 && bit == 64.

How about returning VALUE simply in this case? like the patch below,
diff mbox

Patch

diff --git a/gdb/utils.c b/gdb/utils.c
index 47adb67..83a6df6 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3028,10 +3028,13 @@  gdb_sign_extend (LONGEST value, int bit)
 {
   gdb_assert (bit >= 1 && bit <= 8 * sizeof (LONGEST));
 
-  if (((value >> (bit - 1)) & 1) != 0)
+  if (bit < 8 * sizeof (LONGEST) && ((value >> (bit - 1)) & 1) != 0)
     {
       LONGEST signbit = ((LONGEST) 1) << (bit - 1);
 
+      /* Clear upper bits from bit BIT.  */
+      value &= (signbit << 1) - 1;
+
       value = (value ^ signbit) - signbit;
     }