php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76029 Serious regression in foreach() looping
Submitted: 2018-02-28 16:37 UTC Modified: 2018-06-24 09:25 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: mark dot scherer at spryker dot com Assigned:
Status: No Feedback Package: *General Issues
PHP Version: 7.2.2 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2018-02-28 16:37 UTC] mark dot scherer at spryker dot com
Description:
------------
The recent changes in 7.2 must have introduced a major regression in foreach() looping and variable assignment.

Test script:
---------------
// BROKEN NOW IN PHP7.2
        foreach ($productAttributeKeys as $type) {
            $isDefined = $this->attributeTransferCollection->has($type);
            $shouldBeTextArea = mb_strlen($value) > 255;

            if ($isDefined) {
                continue;
            }

            if ($shouldBeTextArea) {
                $inputType = self::TEXT_AREA_INPUT_TYPE;
            }
            ...
        }

// FIXED WITH: Moving continue statement up
        foreach ($productAttributeKeys as $type) {
            $isDefined = $this->attributeTransferCollection->has($type);
            if ($isDefined) {
                continue;
            }

            $shouldBeTextArea = (mb_strlen($value) > 255);

            if ($shouldBeTextArea === true) {
                $inputType = self::TEXT_AREA_INPUT_TYPE;
            }
            ...
        }

Expected result:
----------------
No notice/error on the most basic

            $shouldBeTextArea = mb_strlen($value) > 255;

            if ($isDefined) {
                continue;
            }

            if ($shouldBeTextArea) {
                $inputType = self::TEXT_AREA_INPUT_TYPE;
            }

Actual result:
--------------
When using continue, variables that must be assigned and fine are suddenly now throwing "Undefined variable: shouldBeTextArea" - this worked in all PHP versions until 7.1 incl.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-02-28 16:50 UTC] peehaa@php.net
-Status: Open +Status: Feedback
 [2018-02-28 16:50 UTC] peehaa@php.net
No repro: https://3v4l.org/fuGjJ

Please provide a simplified and working repro case.
 [2018-02-28 16:54 UTC] peehaa@php.net
Could you also test it with opcache enabled and disabled please and post the results?
 [2018-02-28 17:14 UTC] mark dot scherer at gmx dot de
opcode is on, I will disable and try again

$value = ...;

was a missing line I removed before the "$shouldBeTextArea = mb_strlen($value) > 255;", but it does not change the result/report.
 [2018-02-28 17:19 UTC] mark dot scherer at gmx dot de
OK, so disabled opcache and all is fine, so this at least limits itself to the opcode cache. We will keep it disabled for now - but this will sure kill a lot of php applications once deployed.
 [2018-02-28 22:01 UTC] nikic@php.net
Looking at the code, my best guess is that we're contracting the assignment for $shouldBeTextArea into IS_SMALLER and then something eats the NOP, resulting in a smart branch. But I can't reproduce this. Would be nice to have a self-contained reproducing script for this.

Instead of disabling opcache entirely, it should be enough to set opcache.optimization_level=0 until this is fixed.
 [2018-03-01 13:50 UTC] mark dot scherer at gmx dot de
Yes, using optimization_level=0 also works fine.
Thank you!
 [2018-06-24 04:25 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2018-06-24 09:25 UTC] nikic@php.net
There has been one smart-branch related fix in https://github.com/php/php-src/commit/6738d19fb80b8696f163cae4b9c114c247606daa, which probably resolved this issue as well. Might be worth trying to reenable optimization_level and see if the problem went away.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 23:01:26 2024 UTC