This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[PATCH v2 04/10] Avoid undefined behavior in extract_integer
- From: Tom Tromey <tom at tromey dot com>
- To: gdb-patches at sourceware dot org
- Cc: Tom Tromey <tom at tromey dot com>
- Date: Wed, 29 Aug 2018 20:44:10 -0600
- Subject: [PATCH v2 04/10] Avoid undefined behavior in extract_integer
- References: <20180830024416.23386-1-tom@tromey.com>
-fsanitize=undefined showed that extract_integer could left-shift a
negative value, which is undefined. This patch fixes the problem by
doing all the work in an unsigned type. This relies on
implementation-defined behavior, but I tend to think we are on safe
ground there. (Also, if need be, violations of this could probably be
detected, either by configure or by a static_assert.)
gdb/ChangeLog
2018-08-29 Tom Tromey <tom@tromey.com>
* findvar.c (extract_integer): Do work in an unsigned type.
---
gdb/ChangeLog | 4 ++++
gdb/findvar.c | 2 +-
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/gdb/findvar.c b/gdb/findvar.c
index 9256833ab60..be6c9d6f60b 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -50,7 +50,7 @@ template<typename T, typename>
T
extract_integer (const gdb_byte *addr, int len, enum bfd_endian byte_order)
{
- T retval = 0;
+ typename std::make_unsigned<T>::type retval = 0;
const unsigned char *p;
const unsigned char *startaddr = addr;
const unsigned char *endaddr = startaddr + len;
--
2.13.6