Bug #53934 [Com]: The negative PHP_INT_MAX is incorrectly converted to float

[email protected] ("ajf at ajf dot me")
Newsgroups php.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=53934&edit=1

 ID:                 53934
 Comment by:         ajf at ajf dot me
 Reported by:        eriksen dot costa at infranology dot com dot br
 Summary:            The negative PHP_INT_MAX is incorrectly converted to
                     float
 Status:             Verified
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Linux
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N

 New Comment:

Since PHP 7 there is PHP_INT_MIN for this purpose, by the way.


Previous Comments:
------------------------------------------------------------------------
[2014-06-05 15:12:15] ajf at ajf dot me

OK, I found out why this happens. Because the difference between subtraction and negation can't be known at lexing time ($x = 12-3 vs $x = -3), -9223372036854775808 is actually parsed as -(9223372036854775808). Since signed integers are asymettrical (max is 9223372036854775807 but min is -9223372036854775808), 9223372036854775808 overflows and becomes a float. Then the parser sees -(9.2233720368548E+18) and subtracts it, yielding -9.2233720368548E+18.

I'm not sure how this could be fixed short of parsing numbers at the last minute. Maybe that would actually reap performance benefits!

------------------------------------------------------------------------
[2014-06-05 14:37:19] ajf at ajf dot me

This issue still exists in master.

------------------------------------------------------------------------
[2012-08-09 14:05:43] ajf at ajf dot me

@catphract:

In that case is it not a tokeniser issue, and "-9223372036854775807" should be 
tokenised to '-9223372036854775807' not '-' '9223372036854775807'?

------------------------------------------------------------------------
[2011-02-05 18:55:33] [email protected]

The problem is that "-9223372036854775808" is not parsed as an integer, instead the minus sign and the rest are parsed individually and 9223372036854775808 is bigger than LONG_MAX.

Probably this is not trivial to fix.

------------------------------------------------------------------------
[2011-02-05 14:53:32] eriksen dot costa at infranology dot com dot br

Description:
------------
I found this and seems a bug. Everytime I use the negative PHP_INT_MAX literally (-9223372036854775808) as a function parameter, it is converted to float.

However, if I pass it like an expression -9223372036854775807 - 1, it is correctly identified as an integer.

Test script:
---------------
--TEST--
Checks if the negative PHP_INT_MAX is treated as integer.
--CREDITS--
Eriksen Costa <[email protected]>
--SKIPIF--
<?php
if (PHP_INT_SIZE < 8) die('64bit platforms only');
?>
--FILE--
<?php
var_dump(-9223372036854775807 - 1);
var_dump((PHP_INT_MAX * -1) - 1);
var_dump(-9223372036854775808);
?>
--EXPECT--
int(-9223372036854775808);
int(-9223372036854775808);
int(-9223372036854775808);

Expected result:
----------------
int(-9223372036854775808);
int(-9223372036854775808);
int(-9223372036854775808);

Actual result:
--------------
int(-9223372036854775808)
int(-9223372036854775808)
float(-9.2233720368548E+18)



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=53934&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.