How's this for a counter-proposal?  I shamelessly reused your commit
message and ChangeLog wording, please let me know if that's overstepping
boundaries.

Thanks,
PM

>From 2ed5c470803d15c960a9a2574dc572ac1aa92b71 Mon Sep 17 00:00:00 2001
Message-Id: 
<2ed5c470803d15c960a9a2574dc572ac1aa92b71.1398284498.git.pmach...@redhat.com>
From: Petr Machata <[email protected]>
Date: Wed, 23 Apr 2014 22:19:57 +0200
Subject: [PATCH] libdw (get_sleb128_step): Remove undefined behavior.
Gcc: nnimap+mail.corp.redhat.com:Sent
To: [email protected]

As pointed out by gcc -fsanitize=undefined left shifting a negative
value is undefined. Replace it with an explicit sign extension step.

Signed-off-by: Petr Machata <[email protected]>
---
 libdw/ChangeLog       |    5 +++++
 libdw/memory-access.h |    3 ++-
 2 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 49d70af..bd2f0e7 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,8 @@
+2014-04-23  Petr Machata  <[email protected]>
+
+       * memory-access.h (get_sleb128_step): Remove undefined behavior of
+       left shifting a signed value by casting through unsigned.
+
 2014-04-13  Mark Wielaard  <[email protected]>
 
        * Makefile.am: Remove !MUDFLAP conditions.
diff --git a/libdw/memory-access.h b/libdw/memory-access.h
index d0ee63c..647a2f3 100644
--- a/libdw/memory-access.h
+++ b/libdw/memory-access.h
@@ -71,7 +71,8 @@ __libdw_get_uleb128 (const unsigned char **addrp)
     if (likely ((__b & 0x80) == 0))                                          \
       {                                                                        
      \
        struct { signed int i:7; } __s = { .i = __b };                        \
-       (var) |= (typeof (var)) __s.i << ((nth) * 7);                         \
+       (var) |= (typeof (var))                                               \
+         (((uint64_t) (typeof (var)) __s.i) << ((nth) * 7));                 \
        return (var);                                                         \
       }                                                                        
      \
     (var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7);                     \
-- 
1.7.6.5

Reply via email to