php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64890 strrpos with negative offset incorrect results
Submitted: 2013-05-21 17:53 UTC Modified: 2013-05-31 15:54 UTC
From: me at rouvenwessling dot de Assigned:
Status: Not a bug Package: Strings related
PHP Version: 5.4.15 OS: OS X
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: me at rouvenwessling dot de
New email:
PHP Version: OS:

 

 [2013-05-21 17:53 UTC] me at rouvenwessling dot de
Description:
------------
Apparently the offset in strrpos doesn't work right if it's the same as the needle 
length.

Test script:
---------------
$result = strrpos('Internationalization', 'n', -1);
var_dump($result);

Expected result:
----------------
int(10)

Actual result:
--------------
int(19)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-05-21 23:25 UTC] cmbecker69 at gmx dot de
The issue occurs for negative offsets, 
when the length of the needle is less than 
or equal to the absolute value of the offset.

The cause seems to be in ext/standard/string.c line 1958:

  e = haystack + haystack_len + offset;

This lets e reference one character further to the right
than it actually should. Changing this line to:

  e = haystack + haystack_len + offset - 1;

fixes the issue and gives the expected result for the test script.
 [2013-05-22 07:45 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2013-05-22 07:45 UTC] laruence@php.net
I dont think it's a bug

'Internationalization' , then -1 , the cursor is at 

'Internationalization', exactlly point to the 'n' 
                    ^

so, no matter what direction it try to find in, the "n" should be returned
 [2013-05-31 15:54 UTC] me at rouvenwessling dot de
I'd disagree. If I specify a positive offset that many characters from the 
beginning of the string are ignored. If I specify a negative offset I expect that 
many characters from the end to be ignored. That is also what the documentation 
states:

"If specified, search will start this number of characters counted from the 
beginning of the string. If the value is negative, search will instead start from 
that many characters from the end of the string, searching backwards."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 19:01:28 2024 UTC