php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36944 strncmp & negative len
Submitted: 2006-04-02 00:33 UTC Modified: 2017-06-11 15:20 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: ms419 at freezone dot co dot uk Assigned: laruence (profile)
Status: Closed Package: Strings related
PHP Version: 5.2.6 OS: Debian
Private report: No CVE-ID: None
 [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)

Pull Requests

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
 [2017-06-11 15:16 UTC] php at mcq8 dot be
This is fixed in 5.1.3 https://3v4l.org/Ite0e
The bug can be closed.
 [2017-06-11 15:20 UTC] requinix@php.net
-Status: Open +Status: Closed -Type: Feature/Change Request +Type: Bug -Assigned To: +Assigned To: laruence
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC