Skip to content

Commit

Permalink
Use array_set_zval_key() in zend_ast_add_array_element()
Browse files Browse the repository at this point in the history
This reimplemented basically the same logic.
  • Loading branch information
nikic committed Aug 31, 2021
1 parent 65428d8 commit 7d6a7e7
Showing 1 changed file with 13 additions and 33 deletions.
46 changes: 13 additions & 33 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,41 +436,21 @@ ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *ast, zend_ast *op)

static zend_result zend_ast_add_array_element(zval *result, zval *offset, zval *expr)
{
switch (Z_TYPE_P(offset)) {
case IS_UNDEF:
if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) {
zend_throw_error(NULL,
"Cannot add element to the array as the next element is already occupied");
return FAILURE;
}
break;
case IS_STRING:
zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(offset), expr);
zval_ptr_dtor_str(offset);
break;
case IS_NULL:
zend_symtable_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), expr);
break;
case IS_LONG:
zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(offset), expr);
break;
case IS_FALSE:
zend_hash_index_update(Z_ARRVAL_P(result), 0, expr);
break;
case IS_TRUE:
zend_hash_index_update(Z_ARRVAL_P(result), 1, expr);
break;
case IS_DOUBLE:
zend_hash_index_update(Z_ARRVAL_P(result), zend_dval_to_lval_safe(Z_DVAL_P(offset)), expr);
break;
case IS_RESOURCE:
zend_error(E_WARNING, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(offset), Z_RES_HANDLE_P(offset));
zend_hash_index_update(Z_ARRVAL_P(result), Z_RES_HANDLE_P(offset), expr);
break;
default:
zend_type_error("Illegal offset type");
if (Z_TYPE_P(offset) == IS_UNDEF) {
if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) {
zend_throw_error(NULL,
"Cannot add element to the array as the next element is already occupied");
return FAILURE;
}
return SUCCESS;
}

if (array_set_zval_key(Z_ARRVAL_P(result), offset, expr) == FAILURE) {
return FAILURE;
}

zval_ptr_dtor_nogc(offset);
zval_ptr_dtor_nogc(expr);
return SUCCESS;
}

Expand Down

0 comments on commit 7d6a7e7

Please sign in to comment.