|
|
683572 |
From 7e5b390a008ccad094a39c350f385d58e8a5102a Mon Sep 17 00:00:00 2001
|
|
|
683572 |
From: Karl Williamson <khw@cpan.org>
|
|
|
683572 |
Date: Fri, 3 May 2019 13:57:47 -0600
|
|
|
683572 |
Subject: [PATCH] Remove undefined behavior from IV shifting
|
|
|
683572 |
MIME-Version: 1.0
|
|
|
683572 |
Content-Type: text/plain; charset=UTF-8
|
|
|
683572 |
Content-Transfer-Encoding: 8bit
|
|
|
683572 |
|
|
|
683572 |
It is undefined behavior to shift a negative integer to the left. This
|
|
|
683572 |
commit avoids that by treating the value as unsigned, then casting back
|
|
|
683572 |
to integer for return.
|
|
|
683572 |
|
|
|
683572 |
Petr Písař: Ported to 5.30.0 from
|
|
|
683572 |
814735a391b874af8f00eaf89469e5ec7f38cd4aa.
|
|
|
683572 |
|
|
|
683572 |
Signed-off-by: Petr Písař <ppisar@redhat.com>
|
|
|
683572 |
---
|
|
|
683572 |
asan_ignore | 5 -----
|
|
|
683572 |
pp.c | 21 ++++++++++++++++++++-
|
|
|
683572 |
2 files changed, 20 insertions(+), 6 deletions(-)
|
|
|
683572 |
|
|
|
683572 |
diff --git a/asan_ignore b/asan_ignore
|
|
|
683572 |
index e0f5685..f520546 100644
|
|
|
683572 |
--- a/asan_ignore
|
|
|
683572 |
+++ b/asan_ignore
|
|
|
683572 |
@@ -18,11 +18,6 @@
|
|
|
683572 |
|
|
|
683572 |
fun:Perl_pp_i_*
|
|
|
683572 |
|
|
|
683572 |
-# Perl's << is defined as using the underlying C's << operator, with the
|
|
|
683572 |
-# same undefined behaviour for shifts greater than the word size.
|
|
|
683572 |
-# (UVs normally, IVs with 'use integer')
|
|
|
683572 |
-
|
|
|
683572 |
-fun:Perl_pp_left_shift
|
|
|
683572 |
|
|
|
683572 |
# this function numifies the field width in eg printf "%10f".
|
|
|
683572 |
# It has its own overflow detection, so don't warn about it
|
|
|
683572 |
diff --git a/pp.c b/pp.c
|
|
|
683572 |
index 7afb090..3ca04e1 100644
|
|
|
683572 |
--- a/pp.c
|
|
|
683572 |
+++ b/pp.c
|
|
|
683572 |
@@ -1991,10 +1991,29 @@ static IV S_iv_shift(IV iv, int shift, bool left)
|
|
|
683572 |
shift = -shift;
|
|
|
683572 |
left = !left;
|
|
|
683572 |
}
|
|
|
683572 |
+
|
|
|
683572 |
if (UNLIKELY(shift >= IV_BITS)) {
|
|
|
683572 |
return iv < 0 && !left ? -1 : 0;
|
|
|
683572 |
}
|
|
|
683572 |
- return left ? iv << shift : iv >> shift;
|
|
|
683572 |
+ /* For left shifts, perl 5 has chosen to treat the value as unsigned for
|
|
|
683572 |
+ * the * purposes of shifting, then cast back to signed. This is very
|
|
|
683572 |
+ * different from perl 6:
|
|
|
683572 |
+ *
|
|
|
683572 |
+ * $ perl6 -e 'say -2 +< 5'
|
|
|
683572 |
+ * -64
|
|
|
683572 |
+ *
|
|
|
683572 |
+ * $ ./perl -le 'print -2 << 5'
|
|
|
683572 |
+ * 18446744073709551552
|
|
|
683572 |
+ * */
|
|
|
683572 |
+ if (left) {
|
|
|
683572 |
+ if (iv == IV_MIN) { /* Casting this to a UV is undefined behavior */
|
|
|
683572 |
+ return 0;
|
|
|
683572 |
+ }
|
|
|
683572 |
+ return (IV) (((UV) iv) << shift);
|
|
|
683572 |
+ }
|
|
|
683572 |
+
|
|
|
683572 |
+ /* Here is right shift */
|
|
|
683572 |
+ return iv >> shift;
|
|
|
683572 |
}
|
|
|
683572 |
|
|
|
683572 |
#define UV_LEFT_SHIFT(uv, shift) S_uv_shift(uv, shift, TRUE)
|
|
|
683572 |
--
|
|
|
683572 |
2.20.1
|
|
|
683572 |
|