Skip to content

Commit

Permalink
Fixed bug #76717
Browse files Browse the repository at this point in the history
Print INT_MIN as -INT_MAX-1 to avoid it getting parsed as a float
literal due to integer overflow.
  • Loading branch information
nikic committed Mar 11, 2019
1 parent 769d2d9 commit 1fd32e9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ PHP NEWS
custom wrapper). (Laruence)
. Fixed bug #77669 (Crash in extract() when overwriting extracted array).
(Nikita)
. Fixed bug #76717 (var_export() does not create a parsable value for
PHP_INT_MIN). (Nikita)

07 Mar 2019, PHP 7.2.16

Expand Down
14 changes: 14 additions & 0 deletions ext/standard/tests/general_functions/bug76717.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Bug #76717: var_export() does not create a parsable value for PHP_INT_MIN
--FILE--
<?php

$min = eval('return '.var_export(PHP_INT_MIN, true).';');
$max = eval('return '.var_export(PHP_INT_MAX, true).';');
var_dump($min === PHP_INT_MIN);
var_dump($max === PHP_INT_MAX);

?>
--EXPECT--
bool(true)
bool(true)
7 changes: 7 additions & 0 deletions ext/standard/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,13 @@ PHPAPI void php_var_export_ex(zval *struc, int level, smart_str *buf) /* {{{ */
smart_str_appendl(buf, "NULL", 4);
break;
case IS_LONG:
/* INT_MIN as a literal will be parsed as a float. Emit something like
* -9223372036854775807-1 to avoid this. */
if (Z_LVAL_P(struc) == ZEND_LONG_MIN) {
smart_str_append_long(buf, ZEND_LONG_MIN+1);
smart_str_appends(buf, "-1");
break;
}
smart_str_append_long(buf, Z_LVAL_P(struc));
break;
case IS_DOUBLE:
Expand Down

6 comments on commit 1fd32e9

@remicollet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this break ext/standard/tests/general_functions/var_export_basic1.phpt on 32-bit

061+ -2147483647-1
062+ -2147483647-1
063+ string(13) "-2147483647-1"
061- -2147483648
062- -2147483648
063- string(11) "-2147483648"
086+ -2147483647-1
087+ -2147483647-1
088+ string(13) "-2147483647-1"
086- -2147483648
087- -2147483648
088- string(11) "-2147483648"

This seems to be expected, @nikic can you please check ?

@nikic
Copy link
Member Author

@nikic nikic commented on 1fd32e9 Mar 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, those results are expected, the test expectation just needs to be changed.

@remicollet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also ext/standard/tests/general_functions/var_export-locale.phpt

072+ -2147483647-1
073+ -2147483647-1
074+ string(13) "-2147483647-1"
072- -2147483648
073- -2147483648
074- string(11) "-2147483648"
102+ -2147483647-1
103+ -2147483647-1
104+ string(13) "-2147483647-1"
102- -2147483648
103- -2147483648
104- string(11) "-2147483648"

@remicollet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic thanks for confirming.

I will add a skip condition for 32-bit on those 2 tests (and probably duplicate them for 32-bit which have different output).

@remicollet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests fixed in a467a89

@nikic
Copy link
Member Author

@nikic nikic commented on 1fd32e9 Mar 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Please sign in to comment.