[PATCH 2/3] Simplify and inline get_uleb128 and get_sleb128

Petr Machata pmachata at redhat.com
Wed Apr 23 20:29:05 UTC 2014


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.pmachata at redhat.com>
From: Petr Machata <pmachata at redhat.com>
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: elfutils-devel at lists.fedorahosted.org

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 <pmachata at redhat.com>
---
 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  <pmachata at redhat.com>
+
+	* memory-access.h (get_sleb128_step): Remove undefined behavior of
+	left shifting a signed value by casting through unsigned.
+
 2014-04-13  Mark Wielaard  <mjw at redhat.com>
 
 	* 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


More information about the elfutils-devel mailing list