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