4dad76
From 14d26b44a1d7eee67837ec0ea8fb0368ac6fe33e Mon Sep 17 00:00:00 2001
4dad76
From: Tony Cook <tony@develop-help.com>
4dad76
Date: Tue, 20 Aug 2019 15:43:05 +1000
4dad76
Subject: [PATCH] (perl #134230) don't interpret 0x, 0b when numifying strings
4dad76
MIME-Version: 1.0
4dad76
Content-Type: text/plain; charset=UTF-8
4dad76
Content-Transfer-Encoding: 8bit
4dad76
4dad76
Signed-off-by: Petr Písař <ppisar@redhat.com>
4dad76
---
4dad76
 numeric.c  | 9 +++++++++
4dad76
 t/op/int.t | 5 ++++-
4dad76
 2 files changed, 13 insertions(+), 1 deletion(-)
4dad76
4dad76
diff --git a/numeric.c b/numeric.c
4dad76
index f5eadc8173..fae2eb3c6d 100644
4dad76
--- a/numeric.c
4dad76
+++ b/numeric.c
4dad76
@@ -1551,6 +1551,15 @@ Perl_my_atof3(pTHX_ const char* orig, NV* value, const STRLEN len)
4dad76
         if ((endp = S_my_atof_infnan(aTHX_ s, negative, send, value)))
4dad76
             return endp;
4dad76
 
4dad76
+        /* strtold() accepts 0x-prefixed hex and in POSIX implementations,
4dad76
+           0b-prefixed binary numbers, which is backward incompatible
4dad76
+        */
4dad76
+        if ((len == 0 || len >= 2) && *s == '0' &&
4dad76
+            (isALPHA_FOLD_EQ(s[1], 'x') || isALPHA_FOLD_EQ(s[1], 'b'))) {
4dad76
+            *value = 0;
4dad76
+            return (char *)s+1;
4dad76
+        }
4dad76
+
4dad76
         /* If the length is passed in, the input string isn't NUL-terminated,
4dad76
          * and in it turns out the function below assumes it is; therefore we
4dad76
          * create a copy and NUL-terminate that */
4dad76
diff --git a/t/op/int.t b/t/op/int.t
4dad76
index 7e936da68d..b730ab2672 100644
4dad76
--- a/t/op/int.t
4dad76
+++ b/t/op/int.t
4dad76
@@ -7,7 +7,7 @@ BEGIN {
4dad76
     require Config;
4dad76
 }
4dad76
 
4dad76
-plan 17;
4dad76
+plan 19;
4dad76
 
4dad76
 # compile time evaluation
4dad76
 
4dad76
@@ -83,3 +83,6 @@ SKIP:
4dad76
         cmp_ok($x, "==", int($x), "check $x == int($x)");
4dad76
     }
4dad76
 }
4dad76
+
4dad76
+is(1+"0x10", 1, "check string '0x' prefix not treated as hex");
4dad76
+is(1+"0b10", 1, "check string '0b' prefix not treated as binary");
4dad76
-- 
4dad76
2.21.0
4dad76