Skip to content

Commit

Permalink
Merge pull request #70 from DataValues/int32Decimals
Browse files Browse the repository at this point in the history
Fix integer overflow in DecimalValue
  • Loading branch information
Daniel Kinzler committed Jul 11, 2016
2 parents 37c7f49 + 78725f3 commit a0bcd3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/DataValues/DecimalValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DecimalValue extends DataValueObject {
* Constructs a new DecimalValue object, representing the given value.
*
* @param string|int|float $value If given as a string, the value must match
* QUANTITY_VALUE_PATTERN.
* QUANTITY_VALUE_PATTERN. The leading plus sign is optional.
*
* @throws IllegalValueException
*/
Expand Down Expand Up @@ -91,8 +91,8 @@ private function convertToDecimal( $number ) {
throw new InvalidArgumentException( '$number must not be NAN or INF.' );
}

if ( is_int( $number ) || ( $number === floor( $number ) ) ) {
$decimal = strval( abs( (int)$number ) );
if ( is_int( $number ) || $number === (float)(int)$number ) {
$decimal = strval( (int)abs( $number ) );
} else {
$decimal = trim( number_format( abs( $number ), 100, '.', '' ), '0' );

Expand All @@ -101,7 +101,7 @@ private function convertToDecimal( $number ) {
}

if ( substr( $decimal, -1 ) === '.' ) {
$decimal .= '0';
$decimal = substr( $decimal, 0, -1 );
}
}

Expand Down
12 changes: 11 additions & 1 deletion tests/DataValues/DecimalValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function validConstructorArgumentsProvider() {
$argLists[] = array( 4.2 );
$argLists[] = array( -4.2 );
$argLists[] = array( '+4.2' );
$argLists[] = array( " +4.2\n" );
$argLists[] = array( 0 );
$argLists[] = array( 0.2 );
$argLists[] = array( '-0.42' );
Expand All @@ -41,9 +42,10 @@ public function validConstructorArgumentsProvider() {
$argLists[] = array( '+0' );
$argLists[] = array( '+0.0' );
$argLists[] = array( '+0.000' );
$argLists[] = array( '+1.' . str_repeat( '0', 124 ) );
$argLists[] = array( '+1.0' . str_repeat( ' ', 124 ) );
$argLists[] = array( '4.2' );
$argLists[] = array( ' 4.2' );
$argLists[] = array( " 4.2\n" );

return $argLists;
}
Expand All @@ -57,11 +59,13 @@ public function invalidConstructorArgumentsProvider() {
$argLists[] = array( '--4.2' );
$argLists[] = array( '-+4.2' );
$argLists[] = array( '+-4.2' );
$argLists[] = array( '+/-0' );
$argLists[] = array( '-.42' );
$argLists[] = array( '+.42' );
$argLists[] = array( '.42' );
$argLists[] = array( '.0' );
$argLists[] = array( '-00' );
$argLists[] = array( '−1' );
$argLists[] = array( '+01.2' );
$argLists[] = array( 'x2' );
$argLists[] = array( '2x' );
Expand Down Expand Up @@ -176,6 +180,12 @@ public function getValueProvider() {
$argLists[] = array( new DecimalValue( '-0' ), '+0' );
$argLists[] = array( new DecimalValue( '+0.0' ), '+0.0' );
$argLists[] = array( new DecimalValue( '+0' ), '+0' );
$argLists[] = array( new DecimalValue( 2147483649 ), '+2147483649' );
$argLists[] = array( new DecimalValue( 1000000000000000 ), '+1000000000000000' );
$argLists[] = array(
new DecimalValue( 1 + 1e-14 / 3 ),
'+1.0000000000000033306690738754696212708950042724609375'
);

return $argLists;
}
Expand Down

0 comments on commit a0bcd3c

Please sign in to comment.