|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29075 strnatcmp('foo ', 'foo ') returns -1, should return 0
Submitted: 2004-07-09 17:23 UTC Modified: 2004-07-15 03:47 UTC
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: oliver at burtchen dot com Assigned:
Status: Closed Package: Strings related
PHP Version: 4CVS, 5CVS OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: oliver at burtchen dot com
New email:
PHP Version: OS:


 [2004-07-09 17:23 UTC] oliver at burtchen dot com
strnatcmp('foo ', 'foo ') returns -1 and not 0 as expected. As far as I can see, it depends on the trailling whitespace.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-11 19:27 UTC]
See also bug #19795 (there doesn't seem to be any test case for this function..the one who fixes should add that too, I can't access CVS atm)

 [2004-07-11 22:41 UTC]
The problem is that spaces at the end of the string cause the ap and bp pointers to be looking at strlen+1 here is the patch to prevent that.

Index: ext/standard/strnatcmp.c
RCS file: /repository/php-src/ext/standard/strnatcmp.c,v
retrieving revision 1.9
diff -u -r1.9 strnatcmp.c
--- ext/standard/strnatcmp.c    16 Apr 2003 21:10:29 -0000      1.9
+++ ext/standard/strnatcmp.c    11 Jul 2004 20:38:30 -0000
@@ -153,13 +153,13 @@
                        return +1;

                ++ap; ++bp;
-               if (ap == aend && bp == bend)
+               if (ap >= aend && bp >= bend)
                        /* The strings compare the same.  Perhaps the caller
                           will want to call strcmp to break the tie. */
                        return 0;
-               else if (ap == aend)
+               else if (ap >= aend)
                        return -1;
-               else if (bp == bend)
+               else if (bp >= bend)
                        return 1;

 [2004-07-15 03:47 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Dec 05 06:01:28 2023 UTC