Integer overflows on float to int casts

From: Date: Wed, 18 Jul 2012 22:39:21 +0000
Subject: Integer overflows on float to int casts
Groups: php.internals 
Request: Send a blank email to internals+get-61445@lists.php.net to get a copy of this message
Hi internals! When a large floating point number is cast to an integer we currently have very low-level C behavior (integer overflow and wraparound): $ /c/php-5.4.1/php -r 'var_dump((int) 4000000000);' int(-294967296) $ /c/php-5.4.1/php -r 'var_dump((int) 6000000000);' int(1705032704) As a fun fact, if you do the same thing with a string float the number if clipped instead of wrapped: $ /c/php-5.4.1/php -r 'var_dump((int) "4000000000");' int(2147483647) This also applies to zend_parse_parameters. l arguments are wrapped, L arguments are clipped. In my eyes this kind of behavior has nothing to do in PHP. PHP is a high-level language, it shouldn't exhibit low-level stuff like integer overflows and wraparound. I think that at least for zend_parse_parameters this should be changed. Overflowing float parameters should not be accepted. Instead throw the usual E_WARNING and return FAILURE. I'm not sure though what one should do about the explicit (int) cast. My preference would be to throw a notice and use the clipping behavior. Thoughts? Nikita

« previous php.internals (#61445) next »