* Re: [PATCH] readelf: handle_core_item large right shift triggers undefined behaviour.
@ 2015-09-07 10:54 Mark Wielaard
0 siblings, 0 replies; 4+ messages in thread
From: Mark Wielaard @ 2015-09-07 10:54 UTC (permalink / raw)
To: elfutils-devel
[-- Attachment #1: Type: text/plain, Size: 218 bytes --]
On Fri, 2015-09-04 at 09:33 +0200, Mark Wielaard wrote:
> I prefer the simpler: if (n < sizeof (w) * 8) w >>= n; else w = 0;
> Which simply states what value we expect for w.
I pushed this version to master now.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] readelf: handle_core_item large right shift triggers undefined behaviour.
@ 2015-09-04 7:33 Mark Wielaard
0 siblings, 0 replies; 4+ messages in thread
From: Mark Wielaard @ 2015-09-04 7:33 UTC (permalink / raw)
To: elfutils-devel
[-- Attachment #1: Type: text/plain, Size: 425 bytes --]
On Thu, 2015-09-03 at 13:58 -0700, Roland McGrath wrote:
> What about:
> w >>= n % 32;
> w >>= (n - (n % 32)) % 32;
> ?
I don't think that works. For the interesting case n == 32, you won't
shift at all. Because both expressions (32 % 32) and ((32 - 0) % 32)
evaluate to zero.
I prefer the simpler: if (n < sizeof (w) * 8) w >>= n; else w = 0;
Which simply states what value we expect for w.
Cheers,
Mark
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] readelf: handle_core_item large right shift triggers undefined behaviour.
@ 2015-09-03 20:58 Roland McGrath
0 siblings, 0 replies; 4+ messages in thread
From: Roland McGrath @ 2015-09-03 20:58 UTC (permalink / raw)
To: elfutils-devel
[-- Attachment #1: Type: text/plain, Size: 61 bytes --]
What about:
w >>= n % 32;
w >>= (n - (n % 32)) % 32;
?
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] readelf: handle_core_item large right shift triggers undefined behaviour.
@ 2015-09-03 10:46 Mark Wielaard
0 siblings, 0 replies; 4+ messages in thread
From: Mark Wielaard @ 2015-09-03 10:46 UTC (permalink / raw)
To: elfutils-devel
[-- Attachment #1: Type: text/plain, Size: 2069 bytes --]
The problem is this:
int n = ffs (w);
w >>= n;
The intent is to shift away up to (and including) the first least
significant bit in w. But w is an unsigned int, so 32 bits. And the
least significant bit could be bit 32 (ffs counts from 1). Unfortunately
a right shift equal to (or larger than) the length in bits of the left
hand operand is undefined behaviour. We expect w to be zero afterwards.
Which would terminate the while loop in the function. But since it is
undefined behaviour anything can happen. In this case, what will actually
happen is that w is unchanged, causing an infinite loop...
gcc -fsanitize=undefined will catch and warn about this when w = 0x80000000
https://bugzilla.redhat.com/show_bug.cgi?id=1259259
Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
src/ChangeLog | 4 ++++
src/readelf.c | 12 ++++++++++--
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/src/ChangeLog b/src/ChangeLog
index 5be1075..66f7ead 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2015-09-03 Mark Wielaard <mjw@redhat.com>
+
+ * readelf.c (handle_core_item): Handle right shift >= 32 bits.
+
2015-08-11 Mark Wielaard <mjw@redhat.com>
* elflint.c (check_sections): When gnuld and a NOBITS section falls
diff --git a/src/readelf.c b/src/readelf.c
index d3c2b6b..aab8b5c 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -8474,8 +8474,16 @@ handle_core_item (Elf *core, const Ebl_Core_Item *item, const void *desc,
unsigned int w = negate ? ~*i : *i;
while (w != 0)
{
- int n = ffs (w);
- w >>= n;
+ /* Note that a right shift equal to (or greater than)
+ the number of bits of w is undefined behaviour. In
+ particular when the least significant bit is bit 32
+ (w = 0x8000000) then w >>= n is undefined. So
+ explicitly handle that case separately. */
+ unsigned int n = ffs (w);
+ if (n < sizeof (w) * 8)
+ w >>= n;
+ else
+ w = 0;
bit += n;
if (lastbit != 0 && lastbit + 1 == bit)
--
2.4.3
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-09-07 10:54 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-07 10:54 [PATCH] readelf: handle_core_item large right shift triggers undefined behaviour Mark Wielaard
-- strict thread matches above, loose matches on Subject: below --
2015-09-04 7:33 Mark Wielaard
2015-09-03 20:58 Roland McGrath
2015-09-03 10:46 Mark Wielaard
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).