This is the mail archive of the
elfutils-devel@sourceware.org
mailing list for the elfutils project.
Re: [PATCH 2/3] Simplify and inline get_uleb128 and get_sleb128
- From: Mark Wielaard <mjw at redhat dot com>
- To: elfutils-devel at lists dot fedorahosted dot org
- Date: Wed, 23 Apr 2014 00:04:31 +0200
- Subject: Re: [PATCH 2/3] Simplify and inline get_uleb128 and get_sleb128
On Tue, 2014-04-22 at 08:58 -0700, Richard Henderson wrote:
> On 04/22/2014 08:52 AM, Josh Stone wrote:
> > So in total:
> >
> > struct { signed int i:7; } __s = { .i = __b };
> > (var) |= (typeof (var)) __s.i * ((typeof (var)) 1 << ((nth) * 7));
> >
> > Better?
>
> Yes, that's what I had in mind.
Excellent. I changed the patch as attached. No undefined behavior
detected, all tests PASS.
Thanks,
Mark
>From 9ab5b0eba3343e3a75cdc8a8fd5dea76c7a2d351 Mon Sep 17 00:00:00 2001
From: Mark Wielaard <mjw@redhat.com>
Date: Tue, 22 Apr 2014 16:43:11 +0200
Subject: [PATCH] libdw (get_sleb128_step): Remove undefined behavior.
As pointed out by gcc -fsanitize=undefined left shifting a negative value
is undefined. Replace it with a multiplication of the signed value as
suggested by Richard Henderson and Josh Stone.
Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
libdw/ChangeLog | 5 +++++
libdw/memory-access.h | 4 ++--
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 49d70af..a7b0400 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,8 @@
+2014-04-22 Mark Wielaard <mjw@redhat.com>
+
+ * memory-access.h (get_sleb128_step): Remove undefined behavior
+ of left shifting a signed value. Replace it with a multiplication.
+
2014-04-13 Mark Wielaard <mjw@redhat.com>
* Makefile.am: Remove !MUDFLAP conditions.
diff --git a/libdw/memory-access.h b/libdw/memory-access.h
index d0ee63c..f41f783 100644
--- a/libdw/memory-access.h
+++ b/libdw/memory-access.h
@@ -1,5 +1,5 @@
/* Unaligned memory access functionality.
- Copyright (C) 2000-2013 Red Hat, Inc.
+ Copyright (C) 2000-2014 Red Hat, Inc.
This file is part of elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 2001.
@@ -71,7 +71,7 @@ __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)) __s.i * ((typeof (var)) 1 << ((nth) * 7)); \
return (var); \
} \
(var) |= (typeof (var)) (__b & 0x7f) << ((nth) * 7); \
--
1.7.1