php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #36944 strncmp & negative len
Submitted: 2006-04-02 00:33 UTC Modified: 2011-07-26 11:20 UTC
From: ms419 at freezone dot co dot uk Assigned:
Status: Open Package: Strings related
PHP Version: 5.2.6 OS: Debian
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2006-04-02 00:33 UTC] ms419 at freezone dot co dot uk
Description:
------------
strncmp returns incorrect result given negative len -


fis% php
<?php
var_dump(strncmp('foo_wrapper', '_wrapper', -8));
?>
int(7)
fis% 


expected - int(0)
returned - int(7)

Thanks - Jack


Patches

php-5-3-trunk-strncmp-supporting-negative-length (last revision 2011-07-26 11:21 UTC) by laruence@php.net)
bug36944.phpt (last revision 2011-07-25 11:40 UTC) by laruence@php.net)
php-5-3-strn-case-cmp-supporting-negative-length (last revision 2011-07-25 11:37 UTC) by laruence@php.net)
php-5-3-strncasecmp-supporting-negative-length (last revision 2011-07-25 11:06 UTC) by laruence@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-05 11:37 UTC] tony2001@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.

Negative string length is not allowed in this function.
I just committed the patch, from now you'll get an error and FALSE in this case.
 [2008-11-25 18:45 UTC] ms419 at freezone dot co dot uk
I wish to change this bug report to a feature request. I frequently need to check if a string begins or ends with another string. To check if a string begins with another string, I often use strncmp():

ket% php -r 'var_dump(strncmp("HTTP_ACCEPT", "HTTP_", 5));' 
int(0)
ket% 

- but strncmp() is currently not able to check if a string ends with another string:

ket% php -r 'var_dump(strncmp("foo_wrapper", "_wrapper", -8));'

Warning: Length must be greater than or equal to 0 in Command line code on line 1

Call Stack:
    0.0002      62840   1. {main}() Command line code:0
    0.0002      62952   2. strncmp() Command line code:1

bool(false)
ket% 

I wish strncmp() were able to check if a string ends with another string.
 [2011-04-08 20:54 UTC] jani@php.net
-Package: Feature/Change Request +Package: Strings related
 [2011-07-25 11:06 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: php-5-3-strncasecmp-supporting-negative-length
Revision:   1311592004
URL:        https://bugs.php.net/patch-display.php?bug=36944&patch=php-5-3-strncasecmp-supporting-negative-length&revision=1311592004
 [2011-07-25 11:37 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: php-5-3-strn-case-cmp-supporting-negative-length
Revision:   1311593873
URL:        https://bugs.php.net/patch-display.php?bug=36944&patch=php-5-3-strn-case-cmp-supporting-negative-length&revision=1311593873
 [2011-07-25 11:40 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug36944.phpt
Revision:   1311594024
URL:        https://bugs.php.net/patch-display.php?bug=36944&patch=bug36944.phpt&revision=1311594024
 [2011-07-26 11:21 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: php-5-3-trunk-strncmp-supporting-negative-length
Revision:   1311679261
URL:        https://bugs.php.net/patch-display.php?bug=36944&patch=php-5-3-trunk-strncmp-supporting-negative-length&revision=1311679261
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Mon Apr 21 12:02:07 2014 UTC