04bfb0
From b061e315b4eac4d82edb3ca14581805417a68936 Mon Sep 17 00:00:00 2001
04bfb0
From: Tony Cook <tony@develop-help.com>
04bfb0
Date: Wed, 11 Sep 2019 11:50:23 +1000
04bfb0
Subject: [PATCH] (perl #125557) correctly handle overload for bin/oct floats
04bfb0
MIME-Version: 1.0
04bfb0
Content-Type: text/plain; charset=UTF-8
04bfb0
Content-Transfer-Encoding: 8bit
04bfb0
04bfb0
The hexfp code doesn't check that the shift is 4, and so also
04bfb0
accepts binary and octal fp numbers.
04bfb0
04bfb0
Unfortunately the call to S_new_constant() always passed a prefix
04bfb0
of 0x, so overloading would be trying to parse the wrong number.
04bfb0
04bfb0
Another option is to simply allow only hex floats, though some work
04bfb0
was done in 131894 to improve oct/bin float support.
04bfb0
04bfb0
Petr Písař: Ported to 5.30.1 from
04bfb0
2cb5a7e8af11acb0eca22421ec5a4df7ef18e2a9.
04bfb0
04bfb0
Signed-off-by: Petr Písař <ppisar@redhat.com>
04bfb0
---
04bfb0
 t/op/hexfp.t | 16 +++++++++++++++-
04bfb0
 toke.c       | 21 ++++++++++++++++-----
04bfb0
 2 files changed, 31 insertions(+), 6 deletions(-)
04bfb0
04bfb0
diff --git a/t/op/hexfp.t b/t/op/hexfp.t
04bfb0
index 64f8136..0f239d4 100644
04bfb0
--- a/t/op/hexfp.t
04bfb0
+++ b/t/op/hexfp.t
04bfb0
@@ -10,7 +10,7 @@ use strict;
04bfb0
 
04bfb0
 use Config;
04bfb0
 
04bfb0
-plan(tests => 123);
04bfb0
+plan(tests => 125);
04bfb0
 
04bfb0
 # Test hexfloat literals.
04bfb0
 
04bfb0
@@ -277,6 +277,20 @@ is(0b1p0, 1);
04bfb0
 is(0b10p0, 2);
04bfb0
 is(0b1.1p0, 1.5);
04bfb0
 
04bfb0
+# previously these would pass "0x..." to the overload instead of the appropriate
04bfb0
+# "0b" or "0" prefix.
04bfb0
+fresh_perl_is(<<'CODE', "1", {}, "overload binary fp");
04bfb0
+use overload;
04bfb0
+BEGIN { overload::constant float => sub { return eval $_[0]; }; }
04bfb0
+print 0b0.1p1;
04bfb0
+CODE
04bfb0
+
04bfb0
+fresh_perl_is(<<'CODE', "1", {}, "overload octal fp");
04bfb0
+use overload;
04bfb0
+BEGIN { overload::constant float => sub { return eval $_[0]; }; }
04bfb0
+print 00.1p3;
04bfb0
+CODE
04bfb0
+
04bfb0
 # sprintf %a/%A testing is done in sprintf2.t,
04bfb0
 # trickier than necessary because of long doubles,
04bfb0
 # and because looseness of the spec.
04bfb0
diff --git a/toke.c b/toke.c
04bfb0
index 03c4f2b..3fa20dc 100644
04bfb0
--- a/toke.c
04bfb0
+++ b/toke.c
04bfb0
@@ -10966,6 +10966,7 @@ Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
04bfb0
     const char *lastub = NULL;		/* position of last underbar */
04bfb0
     static const char* const number_too_long = "Number too long";
04bfb0
     bool warned_about_underscore = 0;
04bfb0
+    I32 shift; /* shift per digit for hex/oct/bin, hoisted here for fp */
04bfb0
 #define WARN_ABOUT_UNDERSCORE() \
04bfb0
 	do { \
04bfb0
 	    if (!warned_about_underscore) { \
04bfb0
@@ -11012,8 +11013,6 @@ Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
04bfb0
 	{
04bfb0
 	  /* variables:
04bfb0
 	     u		holds the "number so far"
04bfb0
-	     shift	the power of 2 of the base
04bfb0
-			(hex == 4, octal == 3, binary == 1)
04bfb0
 	     overflowed	was the number more than we can hold?
04bfb0
 
04bfb0
 	     Shift is used when we add a digit.  It also serves as an "are
04bfb0
@@ -11022,7 +11021,6 @@ Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
04bfb0
 	   */
04bfb0
 	    NV n = 0.0;
04bfb0
 	    UV u = 0;
04bfb0
-	    I32 shift;
04bfb0
 	    bool overflowed = FALSE;
04bfb0
 	    bool just_zero  = TRUE;	/* just plain 0 or binary number? */
04bfb0
 	    static const NV nvshift[5] = { 1.0, 2.0, 4.0, 8.0, 16.0 };
04bfb0
@@ -11369,8 +11367,21 @@ Perl_scan_num(pTHX_ const char *start, YYSTYPE* lvalp)
04bfb0
         if (hexfp) {
04bfb0
             floatit = TRUE;
04bfb0
             *d++ = '0';
04bfb0
-            *d++ = 'x';
04bfb0
-            s = start + 2;
04bfb0
+            switch (shift) {
04bfb0
+            case 4:
04bfb0
+                *d++ = 'x';
04bfb0
+                s = start + 2;
04bfb0
+                break;
04bfb0
+            case 3:
04bfb0
+                s = start + 1;
04bfb0
+                break;
04bfb0
+            case 1:
04bfb0
+                *d++ = 'b';
04bfb0
+                s = start + 2;
04bfb0
+                break;
04bfb0
+            default:
04bfb0
+                NOT_REACHED; /* NOTREACHED */
04bfb0
+            }
04bfb0
         }
04bfb0
 
04bfb0
 	/* read next group of digits and _ and copy into d */
04bfb0
-- 
04bfb0
2.21.0
04bfb0