Fix accepting numbers surrounded with a white space

This commit is contained in:
Petr Písař 2017-01-12 10:29:31 +01:00
parent 3d975b2670
commit 799ba7d25e
2 changed files with 175 additions and 1 deletions

View File

@ -0,0 +1,167 @@
From 8835391673fd26d4d739c0b150d9bbaab70af5d6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
Date: Wed, 11 Jan 2017 09:24:21 +0100
Subject: [PATCH] Ignore leading and trailing white spaces for non-decimal
input
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Documentation reads that surrounding white space is ignored. But this
does not work for non-decimal numbers since Math-BigInt-1.999712:
$ perl -MMath::BigInt -e 'print Math::BigInt->new(" 0x1 "), qq{\n}'
NaN
This patch fixes it.
CPAN RT#119805
Signed-off-by: Petr Písař <ppisar@redhat.com>
---
lib/Math/BigFloat.pm | 6 ++++++
lib/Math/BigInt.pm | 6 ++++++
t/from_hex-mbf.t | 15 ++++++++++++++-
3 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/lib/Math/BigFloat.pm b/lib/Math/BigFloat.pm
index 4c2c9b8..f589f00 100644
--- a/lib/Math/BigFloat.pm
+++ b/lib/Math/BigFloat.pm
@@ -4058,6 +4058,7 @@ sub from_hex {
if ($str =~ s/
^
+ \s*
# sign
( [+-]? )
@@ -4084,6 +4085,7 @@ sub from_hex {
( \d+ (?: _ \d+ )* )
)?
+ \s*
$
//x)
{
@@ -4143,6 +4145,7 @@ sub from_oct {
if ($str =~ s/
^
+ \s*
# sign
( [+-]? )
@@ -4166,6 +4169,7 @@ sub from_oct {
( \d+ (?: _ \d+ )* )
)?
+ \s*
$
//x)
{
@@ -4225,6 +4229,7 @@ sub from_bin {
if ($str =~ s/
^
+ \s*
# sign
( [+-]? )
@@ -4251,6 +4256,7 @@ sub from_bin {
( \d+ (?: _ \d+ )* )
)?
+ \s*
$
//x)
{
diff --git a/lib/Math/BigInt.pm b/lib/Math/BigInt.pm
index a50b37e..a08f066 100644
--- a/lib/Math/BigInt.pm
+++ b/lib/Math/BigInt.pm
@@ -3101,12 +3101,14 @@ sub from_hex {
if ($str =~ s/
^
+ \s*
( [+-]? )
(0?x)?
(
[0-9a-fA-F]*
( _ [0-9a-fA-F]+ )*
)
+ \s*
$
//x)
{
@@ -3152,11 +3154,13 @@ sub from_oct {
if ($str =~ s/
^
+ \s*
( [+-]? )
(
[0-7]*
( _ [0-7]+ )*
)
+ \s*
$
//x)
{
@@ -3202,12 +3206,14 @@ sub from_bin {
if ($str =~ s/
^
+ \s*
( [+-]? )
(0?b)?
(
[01]*
( _ [01]+ )*
)
+ \s*
$
//x)
{
diff --git a/t/from_hex-mbf.t b/t/from_hex-mbf.t
index a15e4fd..ad84d5f 100644
--- a/t/from_hex-mbf.t
+++ b/t/from_hex-mbf.t
@@ -3,19 +3,32 @@
use strict;
use warnings;
-use Test::More tests => 27;
+use Test::More tests => 1 + 26 * 4;
my $class;
BEGIN { $class = 'Math::BigFloat'; }
BEGIN { use_ok($class, '1.999710'); }
+my @data;
+my $space = "\t\r\n ";
+
while (<DATA>) {
s/#.*$//; # remove comments
s/\s+$//; # remove trailing whitespace
next unless length; # skip empty lines
my ($in0, $out0) = split /:/;
+
+ push @data, [ $in0, $out0 ],
+ [ $in0 . $space, $out0 ],
+ [ $space . $in0, $out0 ],
+ [ $space . $in0 . $space, $out0 ];
+}
+
+for my $entry (@data) {
+ my ($in0, $out0) = @$entry;
+
my $x;
my $test = qq|\$x = $class -> from_hex("$in0");|;
--
2.7.4

View File

@ -2,12 +2,15 @@ Name: perl-Math-BigInt
%global cpan_version 1.999715
# Keep 4-digit version to compete with perl.spec
Version: %(echo %{cpan_version} | sed 's/\(\.....\)/\1./')
Release: 2%{?dist}
Release: 3%{?dist}
Summary: Arbitrary-size integer and float mathematics
License: GPL+ or Artistic
Group: Development/Libraries
URL: http://search.cpan.org/dist/Math-BigInt/
Source0: http://www.cpan.org/authors/id/P/PJ/PJACKLAM/Math-BigInt-%{cpan_version}.tar.gz
# Fix accepting numbers surrounded with a white space, bug #1412052,
# CPAN RT#119805, fixed in 1.999808
Patch0: Math-BigInt-1.999715-Ignore-leading-and-trailing-white-spaces-for-non-dec.patch
BuildArch: noarch
BuildRequires: coreutils
BuildRequires: findutils
@ -81,6 +84,7 @@ This provides Perl modules for arbitrary-size integer and float mathematics.
%prep
%setup -q -n Math-BigInt-%{cpan_version}
%patch0 -p1
# This core module must be buildable without non-core modules at bootstrap.
%if !%{defined perl_bootstrap}
# Remove bundled modules
@ -110,6 +114,9 @@ make test
%{_mandir}/man3/*
%changelog
* Thu Jan 12 2017 Petr Pisar <ppisar@redhat.com> - 1.9997.15-3
- Fix accepting numbers surrounded with a white space (bug #1412052)
* Thu Feb 04 2016 Fedora Release Engineering <releng@fedoraproject.org> - 1.9997.15-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_24_Mass_Rebuild