]> granicus.if.org Git - php/commitdiff
Fixed bug #72100 (implode() inserts garbage into resulting string when joins very...
authorDmitry Stogov <dmitry@zend.com>
Tue, 26 Apr 2016 10:04:06 +0000 (13:04 +0300)
committerDmitry Stogov <dmitry@zend.com>
Tue, 26 Apr 2016 10:04:06 +0000 (13:04 +0300)
NEWS
ext/standard/string.c
ext/standard/tests/strings/bug72100.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 9330bce21170c690cbce5f467e8f40f89dfafb0b..5e5ae2ff268fba0b9b39833aae0a6b678c0964b8 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,8 @@ PHP                                                                        NEWS
 
 - Core:
   . Fixed bug #72101 (crash on complex code). (Dmitry)
+  . Fixed bug #72100 (implode() inserts garbage into resulting string when
+    joins very big integer). (Mikhail Galanin)
   . Fixed bug #72057 (PHP Hangs when using custom error handler and typehint).
     (Nikita Nefedov)
   . Fixed bug #72038 (Function calls with values to a by-ref parameter don't
index 3f47e73c1d68cca0dce7d77bc215c8a727985677..7ee918c2fe4c8632d88a34073de77b1a931aea27 100644 (file)
@@ -1229,16 +1229,16 @@ PHPAPI void php_implode(const zend_string *delim, zval *arr, zval *return_value)
 
        ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(arr), tmp) {
                if (Z_TYPE_P(tmp) == IS_LONG) {
-                       double val = Z_LVAL_P(tmp);
+                       zend_long val = Z_LVAL_P(tmp);
+
                        *++strptr = NULL;
                        ((zend_long *) (strings + numelems))[strptr - strings] = Z_LVAL_P(tmp);
-                       if (val < 0) {
-                               val = -10 * val;
+                       if (val <= 0) {
+                               len++;
                        }
-                       if (val < 10) {
+                       while (val) {
+                               val /= 10;
                                len++;
-                       } else {
-                               len += (int) log10(10 * (double) val);
                        }
                } else {
                        *++strptr = zval_get_string(tmp);
diff --git a/ext/standard/tests/strings/bug72100.phpt b/ext/standard/tests/strings/bug72100.phpt
new file mode 100644 (file)
index 0000000..7fcc083
--- /dev/null
@@ -0,0 +1,18 @@
+--TEST--
+Test implode() function, problems with big numbers
+--SKIPIF--
+<?php
+if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only");
+?>
+--FILE--
+<?php
+var_dump( implode(" ", ["hello long", 999999999999999999, PHP_INT_MAX]));
+var_dump( implode(" ", ["hello negative long", -999999999999999999, PHP_INT_MIN] ) );
+var_dump( implode(" ", ["hello small long", -101, -100, -99, -90, -11, -10, -9, -1, 0, 1, 2, 9, 10, 11, 90, 99, 100, 101] ) );
+echo "Done\n";
+?>
+--EXPECT--
+string(49) "hello long 999999999999999999 9223372036854775807"
+string(60) "hello negative long -999999999999999999 -9223372036854775808"
+string(76) "hello small long -101 -100 -99 -90 -11 -10 -9 -1 0 1 2 9 10 11 90 99 100 101"
+Done