Re: PHP Integer Bug

From: Date: Sat, 19 Nov 2011 14:46:30 +0000
Subject: Re: PHP Integer Bug
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-56432@lists.php.net to get a copy of this message
On Sat, 19 Nov 2011 13:46:35 -0000, Paul Dragoonis <dragoonis@gmail.com> wrote:
[...] ---- CODE --- $s = "2433078805"; var_dump($s); var_dump((int) $s); exit; ----- EXPECTED ----- string(10) "2433078805" int(2433078805) ----- ACTUAL ----- string(10) "2433078805" int(2147483647)
Anthony has already explained the problem, but I can't find this documented anywhere. The manual says a conversion from a float outside the integer boundaries results in undefined behavior ( http://php.net/manual/en/language.types.integer.php#language.types.integer.casting ) and that "if the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float." ( http://php.net/manual/en/language.types.string.php#language.types.string.conversion ). None of these seems to apply here, though perhaps from combining the two we could conclude the behavior is undefined. Internally, the behavior is also not explicitly laid out; it follows from the behavior of strol. This adds a notice: Index: Zend/zend_operators.c ===================================================================
--- Zend/zend_operators.c       (revision 319548)
+++ Zend/zend_operators.c       (working copy)
@@ -353,7 +353,11 @@
                        {
                                char *strval = Z_STRVAL_P(op);
+                               errno = 0;
                                Z_LVAL_P(op) = strtol(strval, NULL, base);
+                               if (errno == ERANGE) {
+                                       zend_error(E_NOTICE, "Value outside integer bounds");
+                               }
                                STR_FREE(strval);
                        }
                        break;
-- Gustavo Lopes

« previous php.internals (#56432) next »