From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH v2 07/10] Avoid undefined behavior in read_signed_leb128
Date: Thu, 30 Aug 2018 02:44:00 -0000 [thread overview]
Message-ID: <20180830024416.23386-8-tom@tromey.com> (raw)
In-Reply-To: <20180830024416.23386-1-tom@tromey.com>
-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-08-29 Tom Tromey <tom@tromey.com>
* dwarf2read.c (read_signed_leb128): Work in ULONGEST.
---
gdb/ChangeLog | 4 ++++
gdb/dwarf2read.c | 6 +++---
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c
index 86ef1c4040b..e61d6e04cb4 100644
--- a/gdb/dwarf2read.c
+++ b/gdb/dwarf2read.c
@@ -19600,7 +19600,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;
@@ -19612,7 +19612,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)
{
@@ -19620,7 +19620,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;
}
--
2.13.6
next prev parent reply other threads:[~2018-08-30 2:44 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-30 2:44 [PATCH v2 00/10] Enable undefined behavior sanitizer Tom Tromey
2018-08-30 2:44 ` [PATCH v2 01/10] Do not pass NULL to memcpy Tom Tromey
2018-08-30 2:44 ` [PATCH v2 08/10] Avoid undefined behavior in ada_operator_length Tom Tromey
2018-08-30 2:44 ` [PATCH v2 06/10] Avoid undefined behavior in parse_number Tom Tromey
2018-08-30 2:44 ` [PATCH v2 02/10] Change dwarf2_frame_state_reg_info::reg to be std::vector Tom Tromey
2018-08-30 2:44 ` [PATCH v2 04/10] Avoid undefined behavior in extract_integer Tom Tromey
2018-08-30 2:44 ` Tom Tromey [this message]
2018-08-30 2:44 ` [PATCH v2 05/10] Avoid undefined behavior in read_subrange_type Tom Tromey
2018-08-30 2:44 ` [PATCH v2 10/10] Add --enable-ubsan Tom Tromey
2018-08-30 2:44 ` [PATCH v2 09/10] Avoid undefined behavior in expression dumping Tom Tromey
2018-08-30 2:44 ` [PATCH v2 03/10] Use unsigned as base type for some enums Tom Tromey
2018-09-12 11:49 ` [PATCH v2 00/10] Enable undefined behavior sanitizer Tom Tromey
2018-10-02 4:44 [PATCH v2 00/10] Undefined Behavior Sanitizer, this time with docs Tom Tromey
2018-10-02 4:44 ` [PATCH v2 07/10] Avoid undefined behavior in read_signed_leb128 Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180830024416.23386-8-tom@tromey.com \
--to=tom@tromey.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox