This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH] Clear upper bits during sign extension
- From: Yao Qi <yao at codesourcery dot com>
- To: Doug Evans <dje at google dot com>
- Cc: Joel Brobecker <brobecker at adacore dot com>, gdb-patches <gdb-patches at sourceware dot org>
- Date: Mon, 29 Dec 2014 14:26:52 +0800
- Subject: Re: [PATCH] Clear upper bits during sign extension
- Authentication-results: sourceware.org; auth=none
- References: <1419815569-21854-1-git-send-email-yao at codesourcery dot com> <20141229030659 dot GB2123 at adacore dot com> <87d273w2gl dot fsf at codesourcery dot com> <CADPb22S97uP=UbNp4XZvoLtrN0msnFNwEKxoHU88nrDMpwx=rQ at mail dot gmail dot com>
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;
}