public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Simon Marchi <simark@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] gdb: fix undefined behavior reported in copy_bitwise
Date: Sat, 11 Apr 2020 01:07:05 +0000 (GMT)	[thread overview]
Message-ID: <20200411010706.01D34385B835@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=cf83625da29d1239e97f1eb4d145f347cb741889

commit cf83625da29d1239e97f1eb4d145f347cb741889
Author: Artur Shepilko <nomadbyte@gmail.com>
Date:   Fri Apr 10 10:56:43 2020 -0400

    gdb: fix undefined behavior reported in copy_bitwise
    
    gdb version 9.1, built with clang 8.0.0 on Ubuntu 18.04 (x86_64);
    --enable-ubsan (for clang's undefined behavior sanitizer)
    
    Executing command; `maint selftest copy_bitwise` bombs in runtime error:
    ../../gdb/utils.c:3432:28: runtime error: left shift of negative value -1
    
    Closer look reveals the offending shift: `(~0 << nbits)`, apparently 0
    is treated as signed int, resulting in negative complement. Explicitly
    stating it unsigned 0U  fixes it and the `copy_bitwise` test passes
    ok.

Diff:
---
 gdb/ChangeLog | 5 +++++
 gdb/utils.c   | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 661a41467bb..81102ee569b 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2020-04-10  Artur Shepilko  <nomadbyte@gmail.com>
+
+	* utils.c (copy_bitwise): Use unsigned 0 constant as operand of
+	bit shift.
+
 2020-04-10  Tom Tromey  <tromey@adacore.com>
 
 	* symfile.c (symbol_file_add_separate): Preserve OBJF_MAINLINE.
diff --git a/gdb/utils.c b/gdb/utils.c
index bda6bbf5b0e..f5b20331b1e 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -3433,7 +3433,7 @@ copy_bitwise (gdb_byte *dest, ULONGEST dest_offset,
 	buf |= *source << avail;
 
       buf &= (1 << nbits) - 1;
-      *dest = (*dest & (~0 << nbits)) | buf;
+      *dest = (*dest & (~0U << nbits)) | buf;
     }
 }


                 reply	other threads:[~2020-04-11  1:07 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20200411010706.01D34385B835@sourceware.org \
    --to=simark@sourceware.org \
    --cc=gdb-cvs@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).