Skip to content

Conversation

TimWolla
Copy link
Member

PHP Internals Book says:

The ZVAL_DUP macro is similar to ZVAL_COPY, but will duplicate arrays, rather
than just incrementing their refcount. If you are using this macro, you are
almost certainly doing something very wrong.

Replace this by an explicit call to zend_array_dup(), as done in php_array_diff(). Besides being more explicit in what is happening, this likely also results in better assembly.

PHP Internals Book says:

> The ZVAL_DUP macro is similar to ZVAL_COPY, but will duplicate arrays, rather
> than just incrementing their refcount. If you are using this macro, you are
> almost certainly doing something very wrong.

Replace this by an explicit call to `zend_array_dup()`, as done in
`php_array_diff()`. Besides being more explicit in what is happening, this
likely also results in better assembly.
ZEND_PARSE_PARAMETERS_END();

ZVAL_DUP(return_value, array);
RETVAL_ARR(zend_array_dup(Z_ARRVAL_P(array)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a particular reason for not using SEPARATE_ARRAY()? This would avoid a duplication when it's not stricly necessary, e.g.:

$r->shuffleArray(range(0,10));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a specific suggestion as how to adjust the code to leverage SEPARATE_ARRAY? My attempts all lead to either memory unsafety or SEPARATE_ARRAY not providing a value-add.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right indeed, there SEPARATE_ARRAY would not make sense here.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is in any case equivalent because we're taking the IS_ARRAY typecheck path in the ZVAL_DUP macro.

@TimWolla TimWolla merged commit 380f854 into php:master Sep 28, 2024
9 of 10 checks passed
jorgsowa pushed a commit to jorgsowa/php-src that referenced this pull request Oct 1, 2024
)

PHP Internals Book says:

> The ZVAL_DUP macro is similar to ZVAL_COPY, but will duplicate arrays, rather
> than just incrementing their refcount. If you are using this macro, you are
> almost certainly doing something very wrong.

Replace this by an explicit call to `zend_array_dup()`, as done in
`php_array_diff()`. Besides being more explicit in what is happening, this
likely also results in better assembly.
@TimWolla TimWolla deleted the random-no-zval-dup branch May 31, 2025 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants