|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-05-21 17:09 UTC] kazuo at o-ishi dot jp
Description: ------------ The behavior of string comparison using == operator is changed in https://github.com/php/php-src/commit/47db8a9aa19f6e17a1018becf9978315c79a1cb0 to fix bug #54547. This change has a compatibility problem. Before this change (PHP 5.4.3), "01234" == "1234" => TRUE (OK) "09223372036854775808" == "9223372036854775808" => TRUE (compared as float, but result is acceptable) After change, "01234" == "1234" => TRUE (OK) "09223372036854775808" == "9223372036854775808" => FALSE (compared as string) This behavior is not reasonable in that case. New rule is not clear. I think this change should be reverted, before release of 5.4.4. Test script: --------------- echo (("01234" == "1234") ? "true" : "false"), "\n"; echo (("09223372036854775808" == "9223372036854775808") ? "true" : "false"), "\n"; Expected result: ---------------- true true Actual result: -------------- true false PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 15:00:01 2025 UTC |
Hi, This is one more case in 32-bit build: "1234567890123456789" == "12345678901.23456789E8" => TRUE in 5.4.3 FALSE in latest In addition, as I had already reported, (64-bit environment) "9223372036854775808" == "09223372036854775808" => TRUE in 5.4.3 FALSE in latest "9223372036854775808" == " 9223372036854775808" => TRUE in 5.4.3 FALSE in latest (32-bit environment) "9007199254740992" == "9007199254740992." => TRUE in 5.4.3 FALSE in latest "9007199254740992" == " 9007199254740992" => TRUE in 5.4.3 FALSE in latest "9007199254740992" == "09007199254740992" => TRUE in 5.4.3 FALSE in latest ---------------------- I think that NEW RULE of == comparing is difficult to understand and there are some incompatibility from OLD RULE. At least, it is needed to be described explicitly. (Of course, OLD RULE is already complex enough...) OLD RULE: When both strings look like numbers, they are converted to numbers before == comparing. Conversion rule from string to number is described in http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion 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. And NEW RULE?Old comparison ("09223372036854775808" == "9223372036854775808") is a problem since it may lead to md5() hashes accepting wrong passwords. This has very high risk of negative consequences for many PHP users. Thus we decided to fix it ASAP. This change can not be backward compatible - since the whole point was to change how this comparison works. I have very hard time understanding why your code would rely on comparison between two completely distinct strings return "true" - what exactly this code does and why it relates on == comparison truncating long numbers? Maybe if you explain the need better there would be a reason to postpone this change but for now I do not see a reason that would override very real security concern from bug #54547.> Old comparison ("09223372036854775808" == "9223372036854775808") is > a problem since it may lead to md5() hashes accepting wrong > passwords. This has very high risk of negative consequences for many > PHP users. Thus we decided to fix it ASAP. Clearly, it have to be compared using === instead of ==. We should issue the statement at the PHP site: "String to string comparison using == has many problem including security problems. We strongly recommend to use === instead." And we should add new Security section at http://www.php.net/manual/en/security.php In addition, string to string comparison using == should be mentioned as NOT RECOMMENDED explicitly on http://www.php.net/manual/en/language.operators.comparison.php These actions can be done at right now, and it is effective for all PHP users including users of old versions. Fortunately, it's rare the return value is constructed only decimal number since md5() returns 32-character hexdecimal string [(10/16)^32 -> 0.000029% ?]. And, for the application developers, it is comparatively easy to point out and correct a part with such a problem. > This change can not be backward compatible - since the whole point > was to change how this comparison works. Security fix which breaks compatibility in wide and unexpectable area will not work well, because it makes the users difficult to migrate to new version. > I have very hard time understanding why your code would rely on comparison > between two completely distinct strings return "true" - what exactly this code > does and why it relates on == comparison truncating long numbers? That's HISTORICAL REASON. There are many of legacy code with == comparison in the world, they need to work AS IS. If there is such a incompatibility, the maintainers of that code have to check whole code using == operator before upgrading to PHP 5.4.4.Interface to user inputs or external format may rely on current (released version of ) PHP behavior, ignore leading zeros or whitespaces. I'm sorry that I cannot show you simple example. Instead, this is example to show another incompatibility: There is pgsql database table created by CREATE TABLE t (k INT, v NUMERIC(30,1)); This code print "true" on PHP 5.4.3 but "false" on latest. ------------------------------------------------- <?php $key = "1"; $num = "12345678901234567890"; $con = pg_connect("..."); pg_query($con, "DELETE FROM t"); pg_query($con, "INSERT INTO t (k, v) VALUES ($key, $num)"); $rs = pg_query($con, "SELECT v FROM t WHERE k = $key"); $row = pg_fetch_row($rs); if ($row[0] == $num) { echo "true"; } else { echo "false"; } -------------------------------------------------