php.net |  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
Votes:1
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: oliver at burtchen dot com
New email:
PHP Version: OS:

 

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



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-11 19:27 UTC] sniper@php.net
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] curt@php.net
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] iliaa@php.net
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
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC