]> granicus.if.org Git - php/commitdiff
MFH: Fixed bug #45877 (Array key '2147483647' left as string)
authorMatt Wilmas <mattwil@php.net>
Wed, 18 Mar 2009 01:08:12 +0000 (01:08 +0000)
committerMatt Wilmas <mattwil@php.net>
Wed, 18 Mar 2009 01:08:12 +0000 (01:08 +0000)
NEWS
Zend/tests/bug45877.phpt [new file with mode: 0644]
Zend/zend.h
Zend/zend_hash.h
Zend/zend_operators.h

diff --git a/NEWS b/NEWS
index e66cf5055c60e53a74fbd2b53948a65f9c9c9b8d..9ac60f73aac96f8d4772dba46eb53dcf017d0ae4 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -40,6 +40,7 @@ PHP                                                                        NEWS
 - Fixed bug #46347 (parse_ini_file() doesn't support * in keys). (Nuno)
 - Fixed bug #46048 (SimpleXML top-level @attributes not part of iterator).
   (David C.)
+- Fixed bug #45877 (Array key '2147483647' left as string). (Matt)
 - Fixed bug #45432 (PDO: persistent connection leak). (Felipe)
 - Fixed bug #43831 ($this gets mangled when extending PDO with persistent 
   connection). (Felipe)
diff --git a/Zend/tests/bug45877.phpt b/Zend/tests/bug45877.phpt
new file mode 100644 (file)
index 0000000..741bd53
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+Bug #45877 (Array key '2147483647' left as string)
+--FILE--
+<?php
+$keys = array(PHP_INT_MAX,
+       (string) PHP_INT_MAX,
+       (string) (-PHP_INT_MAX - 1),
+       -PHP_INT_MAX - 1,
+       (string) (PHP_INT_MAX + 1));
+
+var_dump(array_fill_keys($keys, 1));
+?>
+--EXPECTF--
+array(3) {
+  [%d7]=>
+  int(1)
+  [-%d8]=>
+  int(1)
+  ["%d8"]=>
+  int(1)
+}
index c270f88740917efd449383f3407baaab34309e50..ed7cc789048e353c08bf82235ba970c04d2ec92d 100644 (file)
@@ -262,6 +262,18 @@ char *alloca ();
 #define LONG_MIN (- LONG_MAX - 1)
 #endif
 
+#if SIZEOF_LONG == 4
+#define MAX_LENGTH_OF_LONG 11
+static const char long_min_digits[] = "2147483648";
+#elif SIZEOF_LONG == 8
+#define MAX_LENGTH_OF_LONG 20
+static const char long_min_digits[] = "9223372036854775808";
+#else
+#error "Unknown SIZEOF_LONG"
+#endif
+
+#define MAX_LENGTH_OF_DOUBLE 32
+
 #undef SUCCESS
 #undef FAILURE
 #define SUCCESS 0
index cbee0d653a29568329016ef493aeef26e722b8a9..698fe4a548dd3cd8ca985bd4ce0c24c352d57ce8 100644 (file)
@@ -313,9 +313,10 @@ END_EXTERN_C()
        }                                                                                                                                                                       \
        if ((*tmp>='0' && *tmp<='9')) do { /* possibly a numeric index */                                       \
                const char *end=key+length-1;                                                                                                                   \
-               long idx;                                                                                                                                               \
+               long idx = end - tmp; /* temp var for remaining length (number of digits) */    \
                                                                                                                                                                                \
-               if (*tmp++=='0' && length>2) { /* don't accept numbers with leading zeros */    \
+               if (idx > MAX_LENGTH_OF_LONG - 1 || (*tmp++ == '0' && length > 2)) {                    \
+                       /* don't accept numbers too long or with leading zeros */                                       \
                        break;                                                                                                                                          \
                }                                                                                                                                                               \
                while (tmp<end) {                                                                                                                               \
@@ -325,17 +326,16 @@ END_EXTERN_C()
                        tmp++;                                                                                                                                          \
                }                                                                                                                                                               \
                if (tmp==end && *tmp=='\0') { /* a numeric index */                                                             \
-                       if (*key=='-') {                                                                                                                        \
-                               idx = strtol(key, NULL, 10);                                                                                    \
-                               if (idx!=LONG_MIN) {                                                                                                    \
-                                       return func;                                                                                                            \
-                               }                                                                                                                                               \
-                       } else {                                                                                                                                        \
-                               idx = strtol(key, NULL, 10);                                                                                    \
-                               if (idx!=LONG_MAX) {                                                                                                    \
-                                       return func;                                                                                                            \
+                       if (idx == MAX_LENGTH_OF_LONG - 1) {                                                                            \
+                               int cmp = strcmp(end - (MAX_LENGTH_OF_LONG - 1), long_min_digits);              \
+                                                                                                                                                                               \
+                               if (!(cmp < 0 || (cmp == 0 && *key == '-'))) {                                                  \
+                                       break;                                                                                                                          \
                                }                                                                                                                                               \
                        }                                                                                                                                                       \
+                                                                                                                                                                               \
+                       idx = strtol(key, NULL, 10);                                                                                            \
+                       return func;                                                                                                                            \
                }                                                                                                                                                               \
        } while (0);                                                                                                                                            \
 }
index 19f3a32461cd38e781dcd9755c9a087516a8ddb5..f549dad1b5aea40f627e4cb82c7f741f86a20ec3 100644 (file)
 #include "ext/bcmath/libbcmath/src/bcmath.h"
 #endif
 
-#if SIZEOF_LONG == 4
-#define MAX_LENGTH_OF_LONG 11
-static const char long_min_digits[] = "2147483648";
-#elif SIZEOF_LONG == 8
-#define MAX_LENGTH_OF_LONG 20
-static const char long_min_digits[] = "9223372036854775808";
-#else
-#error "Unknown SIZEOF_LONG"
-#endif
-
-#define MAX_LENGTH_OF_DOUBLE 32
-
 BEGIN_EXTERN_C()
 ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
 ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);