php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55451 substr_compare NULL length interpreted as 0
Submitted: 2011-08-18 15:55 UTC Modified: 2016-08-07 16:47 UTC
From: datibbaw@php.net Assigned:
Status: Closed Package: Unknown/Other Function
PHP Version: Irrelevant OS: NA
Private report: No CVE-ID: None
 [2011-08-18 15:55 UTC] datibbaw@php.net
Description:
------------
The $length parameter of this function is optional in the documentation, but 
currently it can't be skipped to only set the case sensitivity flag. Values like 
null or false get converted into 0 which causes a warning and the function returns 
false.

Using null to skip the $length parameter would be nice.

Test script:
---------------
<?php

// should output: 0
echo substr_compare("hello World", "world", 6, null, true), PHP_EOL;

?>

Expected result:
----------------
0

Actual result:
--------------
Warning: substr_compare(): The length must be greater than zero

Patches

substr_compare_optlen (last revision 2011-08-18 15:56 UTC by datibbaw@php.net)

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-18 15:56 UTC] datibbaw@php.net
The following patch has been added/updated:

Patch Name: substr_compare_optlen
Revision:   1313682989
URL:        https://bugs.php.net/patch-display.php?bug=55451&patch=substr_compare_optlen&revision=1313682989
 [2016-08-03 09:38 UTC] lauri dot kentta at gmail dot com
Notice that the original test doesn't work anymore, since the NULL is converted to zero length and currently zero length is accepted and will always report equal (empty) substrings.

Better test:

--TEST--
Bug #55451 (substr_compare with NULL as default length)
--FILE--
<?php
var_dump(substr_compare("abcde", "ABCD", 0, NULL, false));   // not equal
var_dump(substr_compare("abcde", "ABCD", 0, NULL, true));    // not equal
var_dump(substr_compare("abcde", "ABCDE", 0, NULL, false));  // not equal
var_dump(substr_compare("abcde", "ABCDE", 0, NULL, true));   // equal
?>
--EXPECT--
int(1)
int(1)
int(1)
int(0)
 [2016-08-07 16:47 UTC] nikic@php.net
-Summary: substr_compare and case sensitivity +Summary: substr_compare NULL length interpreted as 0
 [2016-08-07 16:51 UTC] nikic@php.net
Automatic comment on behalf of lauri.kentta@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e616bc8694f5a2d536462a77d629eca614626c3e
Log: Fix bug #55451
 [2016-08-07 16:51 UTC] nikic@php.net
-Status: Open +Status: Closed
 [2016-10-17 10:10 UTC] bwoebi@php.net
Automatic comment on behalf of lauri.kentta@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=e616bc8694f5a2d536462a77d629eca614626c3e
Log: Fix bug #55451
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC