public inbox for dwz@sourceware.org
 help / color / mirror / Atom feed
* [committed] Fix ubsan triggered in gdb-add-index.sh
@ 2021-03-15 10:55 Tom de Vries
  0 siblings, 0 replies; only message in thread
From: Tom de Vries @ 2021-03-15 10:55 UTC (permalink / raw)
  To: dwz, jakub, mark

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-03-15 10:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-15 10:55 [committed] Fix ubsan triggered in gdb-add-index.sh Tom de Vries

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).