Re: [PATCH] Clear upper bits during sign extension

Yao Qi
2014-12-29T06:27:18Z

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,

--
Yao (齐尧)

gdb:

2014-12-29 Yao Qi <yao@codesourcery.com>

* utils.c (gdb_sign_extend): Do sign extension if BIT is less
than 8 * sizeof (LONGEST). Clear bits from BIT in VALUE.

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;
}