b2938d
From 4e82c85b1c9c9b30253b8624470da6f20a6c0604 Mon Sep 17 00:00:00 2001
b2938d
From: Karl Williamson <khw@cpan.org>
b2938d
Date: Mon, 15 Mar 2021 21:01:47 -0600
b2938d
Subject: [PATCH] Fix broken left shift of IV_MIN under 'use integer'
b2938d
MIME-Version: 1.0
b2938d
Content-Type: text/plain; charset=UTF-8
b2938d
Content-Transfer-Encoding: 8bit
b2938d
b2938d
This fixes GH 18639
b2938d
b2938d
When I wrote this code, I conflated casting and complementing.
b2938d
b2938d
Signed-off-by: Petr Písař <ppisar@redhat.com>
b2938d
---
b2938d
 pp.c       | 3 ---
b2938d
 t/op/bop.t | 9 ++++++++-
b2938d
 2 files changed, 8 insertions(+), 4 deletions(-)
b2938d
b2938d
diff --git a/pp.c b/pp.c
b2938d
index d365afea4c..baf0777a47 100644
b2938d
--- a/pp.c
b2938d
+++ b/pp.c
b2938d
@@ -2007,9 +2007,6 @@ static IV S_iv_shift(IV iv, int shift, bool left)
b2938d
      * 18446744073709551552
b2938d
      * */
b2938d
     if (left) {
b2938d
-        if (iv == IV_MIN) { /* Casting this to a UV is undefined behavior */
b2938d
-            return 0;
b2938d
-        }
b2938d
         return (IV) (((UV) iv) << shift);
b2938d
     }
b2938d
 
b2938d
diff --git a/t/op/bop.t b/t/op/bop.t
b2938d
index 07f057d0a9..31b6531a03 100644
b2938d
--- a/t/op/bop.t
b2938d
+++ b/t/op/bop.t
b2938d
@@ -18,7 +18,7 @@ BEGIN {
b2938d
 # If you find tests are failing, please try adding names to tests to track
b2938d
 # down where the failure is, and supply your new names as a patch.
b2938d
 # (Just-in-time test naming)
b2938d
-plan tests => 502;
b2938d
+plan tests => 503;
b2938d
 
b2938d
 # numerics
b2938d
 ok ((0xdead & 0xbeef) == 0x9ead);
b2938d
@@ -33,6 +33,13 @@ ok ((33023 >> 7) == 257);
b2938d
 # signed vs. unsigned
b2938d
 ok ((~0 > 0 && do { use integer; ~0 } == -1));
b2938d
 
b2938d
+{   # GH #18639
b2938d
+    my $iv_min = -(~0 >> 1) - 1;
b2938d
+    my $shifted;
b2938d
+    { use integer; $shifted = $iv_min << 0 };
b2938d
+    is($shifted, $iv_min, "IV_MIN << 0 yields IV_MIN under 'use integer'");
b2938d
+}
b2938d
+
b2938d
 my $bits = 0;
b2938d
 for (my $i = ~0; $i; $i >>= 1) { ++$bits; }
b2938d
 my $cusp = 1 << ($bits - 1);
b2938d
-- 
b2938d
2.26.3
b2938d