php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47298 Unexpected substr return value when start is negative
Submitted: 2009-02-04 08:40 UTC Modified: 2009-02-05 12:17 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: janne dot salo at exove dot fi Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.2.8 OS: CentOS 5.2
Private report: No CVE-ID: None
 [2009-02-04 08:40 UTC] janne dot salo at exove dot fi
Description:
------------
When substr is given a negative value as $start AND the absolute value of $start is greater than the length of the string, something unexpected occurs: $start seems to be considered equal to 0 in this case.

The documentation states: "If string is less than or equal to start characters long, FALSE will be returned". Strict intepretation would be that if $start is negative, FALSE will never be returned since a negative start can never be greater than the length of the string. However, what you would expect (and what you get in versions older than 5.2.8) is that if the ABSOLUTE value of start is greater than the length of the string, FALSE will be returned. 

Reproduce code:
---------------
substr("1234567", -9, 2);

Expected result:
----------------
The above call returns FALSE in previous PHP versions (tested with 5.2.2 in MacOS X and 5.2.6 in Fedora 8).

Actual result:
--------------
'12'. (Which corresponds to call substr("1234567", 0, 2))

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-04 14:59 UTC] iliaa@php.net
php 5.1.2 returns 12, so there is no BC break here.
 [2009-02-05 12:17 UTC] janne dot salo at exove dot fi
Ran a few more tests, results are
4.4.8 (CentOS 4.2): "12"
4.4.9 (MacOS X Tiger): "12"
5.1.6 (CentOS 5.2): "12"
5.2.0 (Debian 4): "12"
5.2.4 (Fedora 8): FALSE
5.2.6 (RHEL4 and Ubuntu): FALSE

I would still argue there's an issue with some 5.2.x series versions (although I admit now that "12" is probably the intended return value since most versions return that and thus 5.2.8 works correctly and 5.2.2, 5.2.4 and 5.2.6 don't). A whole different question is if "12" is a sensible return value, but let's not go there :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 15:01:30 2024 UTC