Skip to content

Always memoize assert #11686

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
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
24 changes: 16 additions & 8 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4083,10 +4083,6 @@ static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string
zend_op *opline;
uint32_t check_op_number = get_next_op_number();

/* Assert expression may not be memoized and reused as it may not actually be evaluated. */
int orig_memoize_mode = CG(memoize_mode);
CG(memoize_mode) = ZEND_MEMOIZE_NONE;

zend_emit_op(NULL, ZEND_ASSERT_CHECK, NULL, NULL);

if (fbc && fbc_is_finalized(fbc)) {
Expand Down Expand Up @@ -4120,8 +4116,6 @@ static void zend_compile_assert(znode *result, zend_ast_list *args, zend_string
opline = &CG(active_op_array)->opcodes[check_op_number];
opline->op2.opline_num = get_next_op_number();
SET_NODE(opline->result, result);

CG(memoize_mode) = orig_memoize_mode;
} else {
if (!fbc) {
zend_string_release_ex(name, 0);
Expand Down Expand Up @@ -4453,7 +4447,14 @@ static void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{
if (runtime_resolution) {
if (zend_string_equals_literal_ci(zend_ast_get_str(name_ast), "assert")
&& !is_callable_convert) {
zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL);
if (CG(memoize_mode) == ZEND_MEMOIZE_NONE) {
zend_compile_assert(result, zend_ast_get_list(args_ast), Z_STR(name_node.u.constant), NULL);
} else {
/* We want to always memoize assert calls, even if they are positioned in
* write-context. This prevents memoizing their arguments that might not be
* evaluated if assertions are disabled, using a TMPVAR that wasn't initialized. */
zend_compile_memoized_expr(result, ast);
}
} else {
zend_compile_ns_call(result, &name_node, args_ast);
}
Expand All @@ -4472,7 +4473,14 @@ static void zend_compile_call(znode *result, zend_ast *ast, uint32_t type) /* {{

/* Special assert() handling should apply independently of compiler flags. */
if (fbc && zend_string_equals_literal(lcname, "assert") && !is_callable_convert) {
zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc);
if (CG(memoize_mode) == ZEND_MEMOIZE_NONE) {
zend_compile_assert(result, zend_ast_get_list(args_ast), lcname, fbc);
} else {
/* We want to always memoize assert calls, even if they are positioned in
* write-context. This prevents memoizing their arguments that might not be
* evaluated if assertions are disabled, using a TMPVAR that wasn't initialized. */
zend_compile_memoized_expr(result, ast);
}
zend_string_release(lcname);
zval_ptr_dtor(&name_node.u.constant);
return;
Expand Down