|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80285 strspn $length argument may no longer exceed the string bounds
Submitted: 2020-10-26 16:11 UTC Modified: 2020-10-27 10:45 UTC
From: paul dot crovella at gmail dot com Assigned: nikic (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: 8.0.0RC2 OS:
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.
Block user comment
Status: Assign to:
Bug Type:
From: paul dot crovella at gmail dot com
New email:
PHP Version: OS:


 [2020-10-26 16:11 UTC] paul dot crovella at gmail dot com
This commit[1] changed not just out-of-bounds offsets of strspn to throw ValueError, but lengths that exceed the string bounds as well.

Given that a positive $length argument functions as a max-length for the match attempt, the prior behavior of accepting a length beyond the string bounds makes sense and reverting to it would realign with substr whose length parameter serves a similar purpose.

Is something gained by breaking existing code and requiring:

strspn($subject, $mask, $start, min($length, strlen($subject) - $start))


strspn($subject, $mask, $start, $length)

seemed to work fine as intended prior?


Test script:

    strspn("foo", "asdf", 0, 4),
    strspn("fffff", "asdf", 0, 4)

Expected result:

Actual result:
Fatal error: Uncaught ValueError: strspn(): Argument #4 ($length) must be contained in argument #1 ($str) in /in/J05AD:4


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-27 10:45 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2020-10-27 10:45 UTC]
This is the second independent report of this issue, so I've decided to undo the change in The behavior should be strictly identical to doing a substr in advance now.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 12:01:29 2024 UTC