Skip to content
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

Strict comparison for switch cases #3297

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
41 changes: 41 additions & 0 deletions Zend/tests/switch-strict.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
Strict switch cases
--FILE--
<?php

$a = 123;

switch ($a) {
case === '123':
echo "Should not match\n";
break;
case == '123':
echo "Should match\n";
break;
default:
echo "Default\n";
}

switch ($a) {
case === 123.0:
echo "Should not match\n";
break;
case 123.0:
echo "Should match\n";
break;
default:
echo "Default\n";
}

switch ($a) {
case === 123:
echo "Should match\n";
break;
default:
echo "Default\n";
}

--EXPECT--
Should match
Should match
Should match
14 changes: 9 additions & 5 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4988,16 +4988,20 @@ void zend_compile_switch(zend_ast *ast) /* {{{ */

zend_compile_expr(&cond_node, cond_ast);

if (expr_node.op_type == IS_CONST
if (!case_ast->attr
&& expr_node.op_type == IS_CONST
&& Z_TYPE(expr_node.u.constant) == IS_FALSE) {
jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPZ, &cond_node, 0);
} else if (expr_node.op_type == IS_CONST
} else if (!case_ast->attr
&& expr_node.op_type == IS_CONST
&& Z_TYPE(expr_node.u.constant) == IS_TRUE) {
jmpnz_opnums[i] = zend_emit_cond_jump(ZEND_JMPNZ, &cond_node, 0);
} else {
opline = zend_emit_op(NULL,
(expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL,
&expr_node, &cond_node);
zend_uchar equality_op = case_ast->attr;
if (!equality_op) {
equality_op = (expr_node.op_type & (IS_VAR|IS_TMP_VAR)) ? ZEND_CASE : ZEND_IS_EQUAL;
}
opline = zend_emit_op(NULL, equality_op, &expr_node, &cond_node);
Copy link
Member

Choose a reason for hiding this comment

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

Won't you be freeing expr_node many many times here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably, like I said... it's quick and dirty and more for demonstration.
Curiously, my debug build isn't complaining about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, duh, because it's a CV in my tests, I'll bet a TMP_VAR will choke.

SET_NODE(opline->result, &case_node);
if (opline->op1_type == IS_CONST) {
Z_TRY_ADDREF_P(CT_CONSTANT(opline->op1));
Expand Down
8 changes: 8 additions & 0 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,14 @@ case_list:
/* empty */ { $$ = zend_ast_create_list(0, ZEND_AST_SWITCH_LIST); }
| case_list T_CASE expr case_separator inner_statement_list
{ $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, $3, $5)); }
| case_list T_CASE T_IS_EQUAL expr case_separator inner_statement_list
{ zend_ast *case_ast = zend_ast_create(ZEND_AST_SWITCH_CASE, $4, $6);
case_ast->attr = ZEND_IS_EQUAL;
$$ = zend_ast_list_add($1, case_ast); }
| case_list T_CASE T_IS_IDENTICAL expr case_separator inner_statement_list
{ zend_ast *case_ast = zend_ast_create(ZEND_AST_SWITCH_CASE, $4, $6);
case_ast->attr = ZEND_IS_IDENTICAL;
$$ = zend_ast_list_add($1, case_ast); }
| case_list T_DEFAULT case_separator inner_statement_list
{ $$ = zend_ast_list_add($1, zend_ast_create(ZEND_AST_SWITCH_CASE, NULL, $4)); }
;
Expand Down