Bug #72100 [Com]: implode() inserts garbage into resulting string when joins very big integer

From: Date: Mon, 25 Apr 2016 09:13:32 +0000
Subject: Bug #72100 [Com]: implode() inserts garbage into resulting string when joins very big integer
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-200758@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72100&edit=1 ID: 72100 Comment by: mikhail dot galanin at yahoo dot com Reported by: mikhail dot galanin at yahoo dot com Summary: implode() inserts garbage into resulting string when joins very big integer Status: Open Type: Bug Package: Scripting Engine problem Operating System: SLES x86_64 PHP Version: 7.0.5 Block user comment: N Private report: N New Comment: Here is .phpt for the issue: --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 /* Prototype : string vsprintf(string $format , array $args) * Description: Return a formatted string * Source code: ext/standard/formatted_print.c */ /* very long number */ 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"; ?> --EXPECTF-- 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 Previous Comments: ------------------------------------------------------------------------ [2016-04-25 09:09:06] mikhail dot galanin at yahoo dot com Description: ------------ In the implode() implementation, there's an optimisation that operates when input array contains long's. This optimisation relays on the fact that log10(num) returns digit number of string representation of "num". Even though mathematically it is correct there an issue when num consist of more than 14 nines (i.e. 99999999999999999). In this case the log10 is rounded up and actual result gets wrong. Test script: --------------- $a = [ 'INSERT IGNORE INTO ', 'Table', (999999999999999999), PHP_INT_MIN, PHP_INT_MAX ]; $str = implode(' ', $a); echo json_encode(["str" => $str]); Expected result: ---------------- {"str":"INSERT IGNORE INTO Table 999999999999999999 -9223372036854775808 9223372036854775807"} Actual result: -------------- {"str":"INSERT IGNORE INTO \u0000 Table 999999999999999999 -9223372036854775808 9223372036854775807"} ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72100&edit=1

« previous php.bugs (#200758) next »