public inbox for dwz@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: dwz@sourceware.org, jakub@redhat.com, mark@klomp.org
Subject: [committed] Fix ubsan triggered in gdb-add-index.sh
Date: Mon, 15 Mar 2021 11:55:50 +0100	[thread overview]
Message-ID: <20210315105549.GA19888@delia> (raw)

Hi,

When compiling with CFLAGS="-O0 -ggdb3 -fsanitize=undefined"
LDFLAGS="-fsanitize=undefined' and running the testsuite, we run into:
...
dwz.c:573:64: runtime error: left shift of 144 by 24 places cannot be \
  represented in type 'int'
FAIL: src/testsuite/dwz.tests/gdb-add-index.sh
...

The problem is here in buf_read_ule32:
...
static inline uint32_t
buf_read_ule32 (unsigned char *data)
{
  return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
...

The data[3] is an unsigned char, which is promoted to int when used as shift
operand, which causes the shift to be a signed one, which can result in
undefined behaviour when shifting into the sign bit.

Fix this by casting to unsigned int.  Likewise in buf_read_ube32.

Committed to trunk.

Thanks,
- Tom

Fix ubsan triggered in gdb-add-index.sh

2021-03-15  Tom de Vries  <tdevries@suse.de>

	* dwz.c (buf_read_ule32, buf_read_ube32): Avoid undefined signed
	left-shift behaviour.

---
 dwz.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/dwz.c b/dwz.c
index d41e26e..54b4bda 100644
--- a/dwz.c
+++ b/dwz.c
@@ -570,13 +570,15 @@ buf_read_ube16 (unsigned char *data)
 static inline uint32_t
 buf_read_ule32 (unsigned char *data)
 {
-  return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
+  return (data[0] | (data[1] << 8) | (data[2] << 16)
+	  | ((unsigned int)data[3] << 24));
 }
 
 static inline uint32_t
 buf_read_ube32 (unsigned char *data)
 {
-  return data[3] | (data[2] << 8) | (data[1] << 16) | (data[0] << 24);
+  return (data[3] | (data[2] << 8) | (data[1] << 16)
+	  | ((unsigned int)data[0] << 24));
 }
 
 static inline uint64_t

                 reply	other threads:[~2021-03-15 10:55 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=20210315105549.GA19888@delia \
    --to=tdevries@suse.de \
    --cc=dwz@sourceware.org \
    --cc=jakub@redhat.com \
    --cc=mark@klomp.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).