php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #3556 strcasecmp() compares lengths instead of string contents
Submitted: 2000-02-21 17:13 UTC Modified: 2000-05-08 06:28 UTC
From: erw dot dongen at wxs dot nl Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.14 OS: WIN98
Private report: No CVE-ID: None
 [2000-02-21 17:13 UTC] erw dot dongen at wxs dot nl
// Ran into this when using strcasecmp as user-sort function for usort()

echo strcasecmp("B", "AAA"); // results in -1 

// workaround: strcmp( strtolower($a), strtolower($b) )

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-03-05 11:55 UTC] sas at cvs dot php dot net
And the problem is..? The observed behaviour is correct. strcasecmp() returns 0, if the parameters match.
 [2000-03-05 15:04 UTC] hholzgra at cvs dot php dot net
no, it isn't !!!

strcasecmp("B","AAA") should be equal to strcmp("B","AAA") 
at least in the results sign

the code for zend_binary_strcasecmp() 
in Zend/zend_operators.c is broken, the following diff
should fix it:

--- zend_operators.c    2000/03/01 13:59:51     1.53
+++ zend_operators.c    2000/03/05 19:56:43
@@ -1256,20 +1256,19 @@
 ZEND_API int zend_binary_strcasecmp(char *s1, uint len1, char *s2, uint len2)
 {
        unsigned char c1 = 0, c2 = 0;
+       int len;
 
-       if (len1 != len2 || !len1) {
-               return len1 - len2;
-       }
+       len = MIN(len1,len2);
 
-       while (len1--) {
+       while (len--) {
                c1 = tolower(*s1++);
                c2 = tolower(*s2++);
                if (c1 != c2) {
-                       break;
+                       return c1 - c2;
                }
        }
 
-       return c1 - c2;
+       return len1 - len2;
 }                                                                                  

 [2000-03-20 11:45 UTC] askalski at cvs dot php dot net
php3_binary_strcasecmp was erroneously comparing string
size before content.  i commited the fix to php3.
zend still needs to be patched.

 [2000-05-08 06:28 UTC] sas at cvs dot php dot net
Thanks for the patch, I've applied it.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun Jun 28 12:00:02 2026 UTC