Skip to content

Commit e483cbc

Browse files
committed
Use specialised functions in SplFixedArray dimension handlers
This is more efficient than manually dealing with a garbage copy.
1 parent 2244810 commit e483cbc

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

Diff for: ext/spl/spl_fixedarray.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ static zval *spl_fixedarray_object_read_dimension_helper(spl_fixedarray_object *
377377
{
378378
/* we have to return NULL on error here to avoid memleak because of
379379
* ZE duplicating uninitialized_zval_ptr */
380-
if (!offset) {
380+
if (UNEXPECTED(!offset)) {
381381
zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
382382
return NULL;
383383
}
@@ -422,7 +422,7 @@ static zval *spl_fixedarray_object_read_dimension(zend_object *object, zval *off
422422

423423
static void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object *intern, zval *offset, zval *value)
424424
{
425-
if (!offset) {
425+
if (UNEXPECTED(!offset)) {
426426
/* '$array[] = value' syntax is not supported */
427427
zend_throw_error(NULL, "[] operator not supported for SplFixedArray");
428428
return;
@@ -438,10 +438,10 @@ static void spl_fixedarray_object_write_dimension_helper(spl_fixedarray_object *
438438
} else {
439439
/* Fix #81429 */
440440
zval *ptr = &(intern->array.elements[index]);
441-
zval tmp;
442-
ZVAL_COPY_VALUE(&tmp, ptr);
443-
ZVAL_COPY_DEREF(ptr, value);
444-
zval_ptr_dtor(&tmp);
441+
/* This should be guaranteed by the VM handler or argument parsing. */
442+
ZEND_ASSERT(Z_TYPE_P(value) != IS_REFERENCE);
443+
Z_TRY_ADDREF_P(value);
444+
zend_safe_assign_to_variable_noref(ptr, value);
445445
}
446446
}
447447

@@ -472,10 +472,9 @@ static void spl_fixedarray_object_unset_dimension_helper(spl_fixedarray_object *
472472
if (UNEXPECTED(index >= intern->array.size)) {
473473
zend_throw_exception(spl_ce_OutOfBoundsException, "Index invalid or out of range", 0);
474474
} else {
475-
zval garbage;
476-
ZVAL_COPY_VALUE(&garbage, &intern->array.elements[index]);
477-
ZVAL_NULL(&intern->array.elements[index]);
478-
zval_ptr_dtor(&garbage);
475+
zval null = {0};
476+
ZVAL_NULL(&null);
477+
zend_safe_assign_to_variable_noref(&intern->array.elements[index], &null);
479478
}
480479
}
481480

0 commit comments

Comments
 (0)