Narrowing occurred during type inference of ZEND_FETCH_DIM_W
Description
We're developing an ecommerce solution called HikaShop for the Joomla CMS. We've been at it for more than 10 years. Several of our users report a warning happening when upgrading to PHP8 which says: Warning: Narrowing occurred during type inference of ZEND_FETCH_DIM_W. Please file a bug report on bugs.php.net in /xxx/xxx/xxx/administrator/components/com_hikashop/helpers/helper.php on line 1381
You can read the reports here: https://www.hikashop.com/forum/install-update/903846-update-hikashop-php.html
Unfortunately, we're not able to reproduce the warning on our end as it apparently only happens in some rare case. Also, it happens on a line which only has this: include_once($include_file);
It's basically the main line of our main helper file to load all the other files (controllers, models, views, other helpers, etc). So the warning message doesn't help much. We could just add a @ to hide the warning, but since the message says to file a bug report, I'm doing that hoping someone has a better idea.
PHP Version
PHP 8
Operating System
No response
Thanks for the report. Would it be possible to receive some more code context from around the line "helper.php on line 1381". Is this file publicly available on GitHub? Can we see the file for ourselves and try to infer from the code which line could have triggered it?
Here is a zip of the helper.php file helper.zip
@dstogov Would you be able to weigh in your opinion? Do we have enough information to narrow down the problem? I wasn't able to reproduce the issue with a clean installation of HikaShop on PHP 8.1.1, but I assume there are a lot of factors here.
I can't reproduce the problem. The provided helper.php doesn't do any array updates at line 1381.
This warning is emitted during loading (compilation and optimization) of PHP file. It doesn't have to be executed. Try to load this file using command line PHP interpreter, then find and separate the problematic function.
First, as a disclaimer, please note that I know nothing about the PHP engine itself. I use PHP but I don't know its inner workings.
What I understand from your message is that the error happens when a PHP file is required/included and something is wrong with the file being required/included. That's exactly what happens here since line 1381 has an include_once() call. If what I understand is correct, then it means the warning message indicates the path of the file where the require/include is done but not the path of the file being required/included (where the actual problem is). And if so, improving that message in future versions of PHP would help understand why this is happening.
On a side note, one of the users reporting the issue to us mentioned during the weekend ( https://www.hikashop.com/forum/install-update/903846-update-hikashop-php.html#340449 ) that after updating to the latest version of HikaShop, he didn't had the warning anymore. So I suppose that message came from some code somewhere which wasn't compatible with PHP8, but that we've modified the code since then ( probably someone else reported an issue with a better message, on another environment ).
I can't say what is wrong and fix this without a reproduction case. May be the warning message really mentions the incorrect file name.
@hikashop-nicolas Could you get in contact with the user that reported this problem and get the file from their system. As you said, they might be using an older version of that file that has an array operation on that line. I saw that such an operation happens at least once in that file. I don't think it's an include; I think the file is correctly reported, but we are looking at a newer version that doesn't have this problem anymore. Also, it would be good to know the exact PHP version they are using.
The warning is currently emitted at the point of the include, we should probably try to emit it at the point of the problematic opcode.
@dstogov The problem is that I can't give that. Our code base contains thousands of files. And the error message only provides the path of the file doing the include, not the path of the file where the problem is, and thus I don't know which file is the problematic one. @kamil-tekiela I've sent a message to him just in case, but I'm pretty sure that he has the same version of the file as I do, with only the include_once on that line. As @nikic said, I think that the issue is that the warning is not emitted at the correct place and thus it makes it hard to find the problem.
The user reporting the issue just confirmed that he has the same file with only the include_once() call on the line reported by the warning message: https://www.hikashop.com/forum/install-update/903846-update-hikashop-php.html#340522
I've improved the warning message in https://github.com/php/php-src/commit/323f3c6914da3fda8658eb7452117370846b1426 and added an assertion in https://github.com/php/php-src/commit/8e58828e89d3978773544085999f6aaab2d41e46, so we should find these through fuzzing now.
Any news here? Nikita's commit is available as of PHP 8.1.6, so an improved error location should be reported with current PHP versions.
Thanks for that. It's been a while and no one reported the issue to us since then. I've updated the forum thread on our end so that if anyone has the issue, they can update their PHP to the 8.1.6 or newer and we can get the new error message with the improved error location.
Hi, I have the same bug. It will be sometimes reproducible on an float addition. One value from an object and the other from the database. We can't found the real logic.
Hello,
I just faced the issue today. We updated to 8.2.13 a week ago, I don't know if it is related. We were on 8.1.
However, I am unable to reproduce the issue.
I cannot share the project (closed-source company project), but the exception happened on line 2142 here:
@wblondel can you share the source of the whole function or method?
@wblondel can you share the source of the whole function or method?
I'll have to ask my company first. I'll post again by the end of the week.
You may first try, if you can reproduce the failure just with this single function. You don't need to execute it. The failure should occur during compilation. You may also obfuscate it, replacing sensitive name. Finally you may send it directly to me by email or private gist.
We have the same issue since updating to 8.2.13. This code worked fine on version PHP 8.2.11.
This is the code:
class NormalPicklistValueObject
{
public function __construct(
public int $idPicklist,
public string $picklistId,
public int $idProduct,
public ?string $deliveryName,
public ?string $pickingContainerName,
public int $amount,
public int $amountCollected,
public string $alias,
public bool $isPicked,
) {
}
}
/** @var NormalPicklistValueObject[] $picklists */
$picklists = [];
/** @var PicklistProduct $picklistProduct */
foreach ($picklistProducts as $picklistProduct) {
$picklistBatchProduct = $picklistBatchProducts[$picklistProduct->idpicklist] ?? null;
if (isset($picklists[$picklistProduct->idpicklist])) {
$picklists[$picklistProduct->idpicklist]->amount += $picklistProduct->amount;
} else {
$picklists[$picklistProduct->idpicklist] = new NormalPicklistValueObject(
idPicklist: $picklistProduct->picklist->idpicklist,
picklistId: $picklistProduct->picklist->picklistid,
idProduct: $picklistProduct->idproduct,
deliveryName: $picklistProduct->picklist->getDeliveryName(),
pickingContainerName: $picklistProduct->picklist->pickingContainer?->name,
amount: $picklistProduct->amount,
amountCollected: $picklistBatchProduct?->amount_picked ?? 0,
alias: $picklistProduct->picklist->getPicklistAliasForBatch(),
isPicked: false,
);
}
}
return array_values($picklists);
This error occurs on the line with $picklists[$picklistProduct->idpicklist]->amount += $picklistProduct->amount;
If I change the line to the following, it executes fine. As you can see, the amount is of type int, not float as the earlier example.
$picklist = $picklists[$picklistProduct->idpicklist];
$picklist->amount += $picklistProduct->amount;
@casperbakker I need a way to reproduce the problem. The source of a single function should be enough, but I need the whole function. I don't know how to use your code fragment. Wrapping it into a file with <?php doesn't reproduce the problem.
Could you please provide a complete case and check that it really triggers the problems.