From 0101665f864383147448c5871a67286a3f7a9a28 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Sun, 29 Jul 2018 17:16:03 -0600 Subject: [PATCH] Avoid undefined behavior in extract_integer -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-10-03 Tom Tromey * 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/ChangeLog b/gdb/ChangeLog index 36c44932743..5787d44424f 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,7 @@ +2018-10-03 Tom Tromey + + * findvar.c (extract_integer): Do work in an unsigned type. + 2018-10-03 Tom Tromey * common/enum-flags.h (enum_flags::operator~): Add static assert. 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 T extract_integer (const gdb_byte *addr, int len, enum bfd_endian byte_order) { - T retval = 0; + typename std::make_unsigned::type retval = 0; const unsigned char *p; const unsigned char *startaddr = addr; const unsigned char *endaddr = startaddr + len; -- 2.30.2