public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH 3/9] Avoid undefined behavior in extract_integer
Date: Mon, 27 Aug 2018 14:57:00 -0000	[thread overview]
Message-ID: <20180827145620.11055-4-tom@tromey.com> (raw)
In-Reply-To: <20180827145620.11055-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, and then using a static_cast
at the end of the function.  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.)

ChangeLog
2018-08-27  Tom Tromey  <tom@tromey.com>

	* findvar.c (extract_integer): Do work in an unsigned type and
	cast at the end.
---
 gdb/ChangeLog | 5 +++++
 gdb/findvar.c | 4 ++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/gdb/findvar.c b/gdb/findvar.c
index 9256833ab60..f2b84db82a1 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;
@@ -86,7 +86,7 @@ That operation is not available on integers of more than %d bytes."),
       for (; p >= startaddr; --p)
 	retval = (retval << 8) | *p;
     }
-  return retval;
+  return static_cast<T> (retval);
 }
 
 /* Explicit instantiations.  */
-- 
2.13.6

  parent reply	other threads:[~2018-08-27 14:57 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-27 14:57 [PATCH 0/9] Add UBSan to the build Tom Tromey
2018-08-27 14:57 ` [PATCH 2/9] Use unsigned as base type for some enums Tom Tromey
2018-08-27 19:22   ` Simon Marchi
2018-08-27 20:22     ` Tom Tromey
2018-08-27 21:26       ` Simon Marchi
2018-08-28 18:54         ` Pedro Alves
2018-08-28 22:42         ` Tom Tromey
2018-08-29  0:01           ` Tom Tromey
2018-08-27 14:57 ` [PATCH 5/9] Avoid undefined behavior in parse_number Tom Tromey
2018-08-27 14:57 ` [PATCH 1/9] Do not pass NULL to memcpy Tom Tromey
2018-08-27 19:12   ` Simon Marchi
2018-08-28 22:40     ` Tom Tromey
2018-08-27 14:57 ` [PATCH 7/9] Avoid undefined behavior in ada_operator_length Tom Tromey
2018-08-27 14:57 ` [PATCH 6/9] Avoid undefined behavior in read_signed_leb128 Tom Tromey
2018-08-27 14:57 ` Tom Tromey [this message]
2018-08-28 18:39   ` [PATCH 3/9] Avoid undefined behavior in extract_integer Pedro Alves
2018-08-29  0:11     ` Tom Tromey
2018-08-27 14:59 ` [PATCH 4/9] Avoid undefined behavior in read_subrange_type Tom Tromey
2018-08-27 14:59 ` [PATCH 9/9] Add --enable-ubsan Tom Tromey
2018-08-27 14:59 ` [PATCH 8/9] Avoid undefined behavior in expression dumping Tom Tromey
2018-08-28 18:49   ` Pedro Alves
2018-08-29  0:27     ` Tom Tromey
2018-08-28 19:03 ` [PATCH 0/9] Add UBSan to the build Pedro Alves
2018-08-29  0:45   ` Tom Tromey
2018-08-30 18:41     ` Pedro Alves
2018-08-30 18:56       ` Tom Tromey
2018-08-30 19:06         ` Pedro Alves

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=20180827145620.11055-4-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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).