From: Operating system: linux PHP version: Irrelevant Package: Strings related Bug Type: Bug Bug description:substr_compare incorrectly reports equality in some cases
Description: ------------ --- >From manual page: http://www.php.net/function.substr-compare#Description <blockquote> int substr_compare ( string $main_str , string $str , int $offset , int $length ... ) substr_compare() compares main_str from position offset with str up to length characters. Return Values Returns < 0 if main_str from position offset is less than str, > 0 if it is greater than str, and 0 if they are equal. ... </blockquote> --- HOWEVER, if main_str is shorter than str, substr_compare checks only up to the length of main_str. This is NOT how I read the description. I believe the comparison should extend to length characters and render a non-zero value if the two strings differ at any character in the first length characters. WORKAROUND: Use strncmp. {Note that substr_compare advertises itself as "binary safe." Since it is stopping at '\0', it fails to actually BE binary safe.} Test script: --------------- <html><head></head><body> <?php echo 'four comparisons of "/" and "/asd":' . '<br/> substr_compare("/", "/asd", 0, 4) => ' . substr_compare("/", "/asd", 0, 4) . ' <b>(SHOULD BE -3)</b>' . '<br/> substr_compare("/asd", "/", 0, 4) => ' . substr_compare("/asd", "/", 0, 4) . '<br/> strncmp("/", "/asd", 4) => ' . strncmp("/", "/asd", 4) . '<br/> strncmp("/asd", "/", 4) => ' . strncmp("/asd", "/", 4); ?> </body></html> Expected result: ---------------- four comparisons of "/" and "/asd": substr_compare("/", "/asd", 0, 4) => -3 substr_compare("/asd", "/", 0, 4) => 3 strncmp("/", "/asd", 4) => -3 strncmp("/asd", "/", 4) => 3 Actual result: -------------- four comparisons of "/" and "/asd": substr_compare("/", "/asd", 0, 4) => 0 (SHOULD BE -3) substr_compare("/asd", "/", 0, 4) => 3 strncmp("/", "/asd", 4) => -3 strncmp("/asd", "/", 4) => 3 As the script says, the first reported value should be -3. -- Edit bug report at http://bugs.php.net/bug.php?id=54454&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=54454&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=54454&r=trysnapshot53 Try a snapshot (trunk): http://bugs.php.net/fix.php?id=54454&r=trysnapshottrunk Fixed in SVN: http://bugs.php.net/fix.php?id=54454&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=54454&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=54454&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=54454&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=54454&r=needscript Try newer version: http://bugs.php.net/fix.php?id=54454&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=54454&r=support Expected behavior: http://bugs.php.net/fix.php?id=54454&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=54454&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=54454&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=54454&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54454&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=54454&r=dst IIS Stability: http://bugs.php.net/fix.php?id=54454&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=54454&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=54454&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=54454&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=54454&r=mysqlcfg