Skip to content

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

Closed
@TRowbotham

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

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