Skip to content

strtok() fails to tokenize the input when passed a literal string when opcache is enabled #13145

Closed

Description

@TRowbotham

Description

The following code:

<?php
$tok = strtok("This is\tan example\nstring", " \n\t");

while ($tok !== false) {
    $tok = strtok(" \n\t");
}

Resulted in this output:

Infinite loop

But I expected this output instead:

Not an infinite loop

The above code results in an infinite loop on PHP 8.3.x and master when opcache is enabled. It works as expected on PHP 8.1.x and 8.2.x with opcache enabled. It also works as expected when opcache is disabled on PHP 8.3.x and master.

If you change the example to use a variable as input instead of a literal string, then it works as expected on PHP 8.3.x and master when opcache is enabled.

<?php
$string = "This is\tan example\nstring";
$tok = strtok($string, " \n\t");

while ($tok !== false) {
    $tok = strtok(" \n\t");
}

PHP Version

PHP 8.3.1

Operating System

Ubuntu 20.04/WSL

Activity

  1. iluuu1994 commented on Jan 14, 2024

    @iluuu1994
    Member

    Hi @TRowbotham! Thanks for the report. This seems to be optimization-related, since opcache.optimization_level=0 dodges the issue. I'll have a look today.

  2. added 2 commits that reference this issue on Jan 14, 2024
    e266e6a
    ed64949
  3. SjonHortensius commented on Feb 13, 2024

    @SjonHortensius
    Contributor

    @iluuu1994 your fix seems incomplete - with 8.3.2 we are seeing other strange issues with strtok and state interference.

    The above testcase (still) fails on 8.3.2 when running under php-fpm

  4. iluuu1994 commented on Feb 13, 2024

    @iluuu1994
    Member

    @SjonHortensius I think this commit is only scheduled for 8.3.3.

  5. SjonHortensius commented on Feb 13, 2024

    @SjonHortensius
    Contributor

    @SjonHortensius I think this commit is only scheduled for 8.3.3.

    thanks - in that case we are experiencing another strtok issue wrt global state that only appeared in 8.3.2 but we're having trouble creating a testcase unfortunately. Will post a separate issue if we do

  6. iluuu1994 commented on Feb 13, 2024

    @iluuu1994
    Member

    @SjonHortensius It's very likely that these are related. Can you try on 8.3.3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions