Skip to content

Attempt to fix GH-10008: Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT #10294

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions Zend/Optimizer/zend_inference.c
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,21 @@ ZEND_API uint32_t zend_array_element_type(uint32_t t1, zend_uchar op_type, int w
return tmp;
}

static zend_always_inline uint32_t array_type_to_kind(uint32_t arr_type)
{
/* Rules:
* HASH_ONLY -> MAY_BE_ARRAY_NUMERIC_HASH
* PACKED_ONLY -> MAY_BE_ARRAY_NUMERIC_HASH | MAY_BE_ARRAY_PACKED (== MAY_BE_ARRAY_KEY_LONG)
* HASH || PACKED -> MAY_BE_ARRAY_NUMERIC_HASH | MAY_BE_ARRAY_PACKED (== MAY_BE_ARRAY_KEY_LONG)
* 0 -> MAY_BE_ARRAY_NUMERIC_HASH
*/
if (MAY_BE_PACKED(arr_type)) {
return MAY_BE_ARRAY_KEY_LONG;
} else {
return MAY_BE_ARRAY_NUMERIC_HASH;
}
}

static uint32_t assign_dim_array_result_type(
uint32_t arr_type, uint32_t dim_type, uint32_t value_type, zend_uchar dim_op_type) {
uint32_t tmp = 0;
Expand All @@ -1939,13 +1954,13 @@ static uint32_t assign_dim_array_result_type(
if (arr_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
tmp |= MAY_BE_ARRAY_PACKED;
}
tmp |= MAY_BE_HASH_ONLY(arr_type) ? MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
tmp |= array_type_to_kind(arr_type);
} else {
if (dim_type & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
if (arr_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
tmp |= MAY_BE_ARRAY_PACKED;
}
tmp |= MAY_BE_HASH_ONLY(arr_type) ? MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
tmp |= array_type_to_kind(arr_type);
}
if (dim_type & MAY_BE_STRING) {
tmp |= MAY_BE_ARRAY_KEY_STRING;
Expand All @@ -1954,7 +1969,7 @@ static uint32_t assign_dim_array_result_type(
if (arr_type & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
tmp |= MAY_BE_ARRAY_PACKED;
}
tmp |= MAY_BE_HASH_ONLY(arr_type) ? MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
tmp |= array_type_to_kind(arr_type);
}
}
if (dim_type & (MAY_BE_UNDEF|MAY_BE_NULL)) {
Expand Down Expand Up @@ -3254,17 +3269,15 @@ static zend_always_inline int _zend_update_type_info(
key_type |= MAY_BE_ARRAY_PACKED;
}
if (t1 & MAY_BE_ARRAY) {
key_type |= MAY_BE_HASH_ONLY(t1) ?
MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
key_type |= array_type_to_kind(t1);
}
} else {
if (t2 & (MAY_BE_LONG|MAY_BE_FALSE|MAY_BE_TRUE|MAY_BE_RESOURCE|MAY_BE_DOUBLE)) {
if (t1 & (MAY_BE_UNDEF|MAY_BE_NULL|MAY_BE_FALSE)) {
key_type |= MAY_BE_ARRAY_PACKED;
}
if (t1 & MAY_BE_ARRAY) {
key_type |= MAY_BE_HASH_ONLY(t1) ?
MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
key_type |= array_type_to_kind(t1);
}
}
if (t2 & MAY_BE_STRING) {
Expand All @@ -3275,8 +3288,7 @@ static zend_always_inline int _zend_update_type_info(
key_type |= MAY_BE_ARRAY_PACKED;
}
if (t1 & MAY_BE_ARRAY) {
key_type |= MAY_BE_HASH_ONLY(t1) ?
MAY_BE_ARRAY_NUMERIC_HASH : MAY_BE_ARRAY_KEY_LONG;
key_type |= array_type_to_kind(t1);
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions ext/opcache/tests/opt/gh10008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
GH-10008 (Narrowing occurred during type inference of ZEND_ADD_ARRAY_ELEMENT)
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.optimization_level=0x20
--EXTENSIONS--
opcache
--FILE--
<?php

function test()
{
$bool = true;
for ($i = 0; $i < 10; $i++) {
if ($bool !== true) {
$string_key = "a";
// The following line triggers narrowing during type inference of ZEND_ADD_ARRAY_ELEMENT.
$array = ["b" => $bool, $string_key => 123];
}

$bool = false;
}
}

echo "Done\n";

?>
--EXPECT--
Done