Skip to content

Fix circumvented type check with return by ref + finally #19172

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

iluuu1994
Copy link
Member

Fixes GH-18736

@@ -5703,6 +5703,15 @@ static void zend_compile_return(zend_ast *ast) /* {{{ */

zend_handle_loops_and_finally((expr_node.op_type & (IS_TMP_VAR | IS_VAR)) ? &expr_node : NULL);

/* Content of reference might have changed in finally, repeat type check. */
if (by_ref
&& !zend_stack_is_empty(&CG(loop_var_stack))
Copy link
Member

Choose a reason for hiding this comment

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

Why is this check needed? The by_ref makes sense because that is how the type of the returned variable changes, and the is_generator and ZEND_ACC_HAS_RETURN_TYPE make sense because those are the conditions used above, but this condition (empty loop variable stack) isn't checked above - maybe add a comment about why it is needed here but not previously?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's just a fast way to check whether we're in a try. We could use zend_has_finally(), but technically even loop/switch var FREEs could change the content of the reference. It's very unlikely to matter in practice, but given return-by-ref with finally/loops should be rare enough I didn't bother optimizing this more.

@DanielEScherzer
Copy link
Member

Should this be targeting 8.3 rather than master?

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.

Returning by ref + finally allow circumventing return type checks
2 participants