Skip to content

match expression matches first expression within a function/method using apache2handler sapi #11134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
myersem2 opened this issue Apr 26, 2023 · 5 comments

Comments

@myersem2
Copy link

myersem2 commented Apr 26, 2023

Description

This issue is visible on Apache/2.4.52, the cli works as expected however something with the sapi (apache2handler) gives you bad results. If you run the following code you will get the correct results on the first page load however if you refresh the page you will get the document incorrect matches below.

The following code:

<test.php>

<?php

// TEST-1: Functioning as expected - PASS
$subj = $unset_variable ?? null;

$result = match ($subj) {
    'Some-Text' => 'Incorect-Match-1',
    'Other-Text' => 'Incorect-Match-2',
    default => 'Expected-Result',
};

var_dump($result); // string 'Expected-Result' (length=15)


// TEST-2: Incorrectly matches first expression - FAILS
function testFunction1()
{
    $subj = $unset_variable ?? null;

    $result = match ($subj) {
        'Some-Text' => 'Incorect-Match-1',
        'Other-Text' => 'Incorect-Match-2',
        default => 'Expected-Result',
    };

    var_dump($result); // string 'Incorect-Match-1' (length=16)
}
testFunction1();


// TEST-3: Incorrectly matches first expression - FAILS
class TestClass1
{
    public function __construct()
    {
        $subj = $unset_variable ?? null;

        $result = match ($subj) {
            'Some-Text' => 'Incorect-Match-1',
            'Other-Text' => 'Incorect-Match-2',
            default => 'Expected-Result',
        };

        var_dump($result); // string 'Incorect-Match-1' (length=16)
    }
}
$test_class = new TestClass1();


// TEST-4: Functioning as expected - PASS
function testFunction2()
{
    $subj = $unset_variable ?? null;

    $result = match ($subj) {
        'Some-Text' => 'Incorect-Match-1',
        //'Other-Text' => 'Incorect-Match-2',
        default => 'Expected-Result',
    };

    var_dump($result); // string 'Expected-Result' (length=15)
}
testFunction2();


// TEST-5: Functioning as expected - PASS
class TestClass2
{
    public function __construct()
    {
        $subj = $unset_variable ?? null;

        $result = match ($subj) {
            'Some-Text' => 'Incorect-Match-1',
            //'Other-Text' => 'Incorect-Match-2',
            default => 'Expected-Result',
        };

        var_dump($result); // string 'Expected-Result' (length=15)
    }
}
$test_class = new TestClass2();


### PHP Version

PHP 8.1.2

### Operating System

Ubuntu 22.04.1
@damianwadley
Copy link
Member

What if you disable opcache?

@zeriyoshi
Copy link
Contributor

This is a bug in OPcache.

root@4f91002b1115:/# php -v
PHP 8.1.18 (cli) (built: Apr 14 2023 18:41:55) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.18, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.18, Copyright (c), by Zend Technologies
root@4f91002b1115:/# php --re "zend opcache" | head
Extension [ <persistent> extension #34 Zend OPcache version 8.1.18 ] {

  - INI {
    Entry [ opcache.enable <ALL> ]
      Current = '1'
    }
    Entry [ opcache.use_cwd <SYSTEM> ]
      Current = '1'
    }
    Entry [ opcache.validate_timestamps <ALL> ]
root@4f91002b1115:/# php test.php
string(15) "Expected-Result"
string(15) "Expected-Result"
string(15) "Expected-Result"
string(15) "Expected-Result"
string(15) "Expected-Result"
root@4f91002b1115:/# php -dopcache.enable_cli=1 test.php
string(15) "Expected-Result"
string(16) "Incorect-Match-1"
string(16) "Incorect-Match-1"
string(15) "Expected-Result"
string(15) "Expected-Result"

@myersem2
Copy link
Author

myersem2 commented Apr 26, 2023

I forgot to mention above if you comment out the second expression it works as expected. Also if you don't use the ?? operator it also works correctly.

function testFunction1()
{
    $subj = $unset_variable ?? null;

    $result = match ($subj) {
        'Some-Text' => 'Incorect-Match-1',
        'Other-Text' => 'Incorect-Match-2',
        default => 'Expected-Result',
    };

    var_dump($result); // string 'Incorect-Match-1' (length=16)
}
testFunction1();

function testFunction2()
{
    $subj = null;

    $result = match ($subj) {
        'Some-Text' => 'Incorect-Match-1',
        'Other-Text' => 'Incorect-Match-2',
        default => 'Expected-Result',
    };

    var_dump($result); // string 'Expected-Result' (length=15)
}
testFunction2();

function testFunction3()
{
    $subj = $unset_variable ?? null;

    $result = match ($subj) {
        'Some-Text' => 'Incorect-Match-1',
        //'Other-Text' => 'Incorect-Match-2',
        default => 'Expected-Result',
    };

    var_dump($result); // string 'Expected-Result' (length=15)
}
testFunction3();

Hopefully that will help pin it down.

@iluuu1994
Copy link
Member

Oof. Thanks for the report. The fix is here: #11135

iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Apr 26, 2023
@myersem2
Copy link
Author

Dang fast response and patch... great work! - thanks

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

No branches or pull requests

5 participants
@myersem2 @iluuu1994 @zeriyoshi @damianwadley and others