Bug 26239: Do not format large negative numbers
authorNick Clemens <nick@bywatersolutions.com>
Tue, 18 Aug 2020 16:41:09 +0000 (16:41 +0000)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 19 Aug 2020 06:22:02 +0000 (08:22 +0200)
At several places we got the following error if we use numbers too big
for Number::Format
Template process failed: undef error - round() overflow. Try smaller
precision or use Math::BigFloat at /home/koha/src/Koha/Number/Price.pm line 44

It make the app explodes.
The goal here is to handle these errors gracefully and easily.

We fixed it for positive numbers in bug 15770, but we neglected the case of negative numbers

Test plan:
- Add a manual credit to a patron of 100000000000000
- ISE!
- Apply patch
- Restart all the things

Works perfectly.

Signed-off-by: Amit Gupta <amit.gupta@informaticsglobal.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Koha/Number/Price.pm
t/Number/Price.t

index 7b0b826..e9a6aa7 100644 (file)
@@ -40,11 +40,10 @@ sub format {
     return unless defined $self->value;
 
     my $format_params = $self->_format_params( $params );
-
     # To avoid the system to crash, we will not format big number
     # We divide per 100 because we want to keep the default DECIMAL_DIGITS (2)
     # error - round() overflow. Try smaller precision or use Math::BigFloat
-    return $self->value if $self->value > Number::Format::MAX_INT/100;
+    return $self->value if abs($self->value) > Number::Format::MAX_INT/100;
 
     return Number::Format->new(%$format_params)->format_price($self->value);
 }
index 141f00f..ce607a8 100644 (file)
@@ -1,6 +1,6 @@
 use Modern::Perl;
 
-use Test::More tests => 34;
+use Test::More tests => 35;
 
 use Test::MockModule;
 use t::lib::Mocks;
@@ -37,6 +37,7 @@ is( Koha::Number::Price->new(1234567890)->format( $format ),
     '1,234,567,890.00', 'US: format 1234567890' );
 
 is( Koha::Number::Price->new(100000000000000)->format, '100000000000000', 'Numbers too big are not formatted');
+is( Koha::Number::Price->new(-100000000000000)->format, '-100000000000000', 'Negative numbers too big are not formatted');
 
 is( Koha::Number::Price->new->format( { %$format, with_symbol => 1 } ),
     '$0.00', 'US: format 0 with symbol' );