php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #72100
Patch implode-log10-bug-fix02 revision 2016-04-25 09:12 UTC by mikhail dot galanin at yahoo dot com
Patch implode-log10-bug-fix01 revision 2016-04-25 09:11 UTC by mikhail dot galanin at yahoo dot com

Patch implode-log10-bug-fix01 for Scripting Engine problem Bug #72100

Patch version 2016-04-25 09:11 UTC

Return to Bug #72100 | Download this patch
Patch Revisions:

Developer: mikhail.galanin@yahoo.com

diff --git a/ext/standard/string.c b/ext/standard/string.c
index 45f2de4..1ab1858 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -1229,17 +1229,17 @@ 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 < 10) {
-				len++;
-			} else {
-				len += (int) log10(10 * (double) val);
-			}
+			++len;
+			if (val < -10) { val /= 10; len += 1; } // minus & prevent overflow
+			if (val < 0) { val = -val; len += 1; } // minus
+			if (val >= 10000000000000000) { val /= 10000000000000000; len += 16; }
+			if (val >= 100000000) { val /= 100000000; len += 8; }
+			if (val >= 10000) { val /= 10000; len += 4; }
+			if (val >= 100) { val /= 100; len += 2; }
+			if (val >= 10) { val /= 10; len += 1; }
 		} else {
 			*++strptr = zval_get_string(tmp);
 			len += ZSTR_LEN(*strptr);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Oct 05 00:01:29 2024 UTC