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