From 56f1106162cdca2cdebfd9321545cc448da32d21 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 14 Dec 2015 17:50:20 +0100 Subject: [PATCH] Use FETCH_CLASS_EXCEPTION for instanceof This does not collide with NO_AUTOLOAD -- missing classes will be silenced, but invalid use of self etc will result in an exception instead of a fatal error. --- Zend/tests/self_instanceof_outside_class.phpt | 17 +++++++++++++++++ Zend/zend_compile.c | 3 ++- Zend/zend_vm_def.h | 1 + Zend/zend_vm_execute.h | 6 ++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 Zend/tests/self_instanceof_outside_class.phpt diff --git a/Zend/tests/self_instanceof_outside_class.phpt b/Zend/tests/self_instanceof_outside_class.phpt new file mode 100644 index 0000000000..4caef37883 --- /dev/null +++ b/Zend/tests/self_instanceof_outside_class.phpt @@ -0,0 +1,17 @@ +--TEST-- +instanceof self outside a class +--FILE-- +getMessage(), "\n"; + } +}; +$fn(); + +?> +--EXPECT-- +Cannot access self:: when no class scope is active diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index ce9477f588..e8752788db 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -6545,7 +6545,8 @@ void zend_compile_instanceof(znode *result, zend_ast *ast) /* {{{ */ "instanceof expects an object instance, constant given"); } - zend_compile_class_ref_ex(&class_node, class_ast, ZEND_FETCH_CLASS_NO_AUTOLOAD); + zend_compile_class_ref_ex(&class_node, class_ast, + ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_EXCEPTION); opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, &obj_node, NULL); diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index ce8554107f..eccb65a3ae 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -7173,6 +7173,7 @@ ZEND_VM_C_LABEL(try_instanceof): ce = zend_fetch_class(NULL, opline->op2.num); if (UNEXPECTED(ce == NULL)) { ZEND_ASSERT(EG(exception)); + FREE_OP1(); HANDLE_EXCEPTION(); } } else { diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 8c15ff93fe..71438dd01d 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -33206,6 +33206,7 @@ try_instanceof: ce = zend_fetch_class(NULL, opline->op2.num); if (UNEXPECTED(ce == NULL)) { ZEND_ASSERT(EG(exception)); + HANDLE_EXCEPTION(); } } else { @@ -34096,6 +34097,7 @@ try_instanceof: ce = zend_fetch_class(NULL, opline->op2.num); if (UNEXPECTED(ce == NULL)) { ZEND_ASSERT(EG(exception)); + HANDLE_EXCEPTION(); } } else { @@ -35496,6 +35498,7 @@ try_instanceof: ce = zend_fetch_class(NULL, opline->op2.num); if (UNEXPECTED(ce == NULL)) { ZEND_ASSERT(EG(exception)); + HANDLE_EXCEPTION(); } } else { @@ -42855,6 +42858,7 @@ try_instanceof: ce = zend_fetch_class(NULL, opline->op2.num); if (UNEXPECTED(ce == NULL)) { ZEND_ASSERT(EG(exception)); + zval_ptr_dtor_nogc(free_op1); HANDLE_EXCEPTION(); } } else { @@ -43188,6 +43192,7 @@ try_instanceof: ce = zend_fetch_class(NULL, opline->op2.num); if (UNEXPECTED(ce == NULL)) { ZEND_ASSERT(EG(exception)); + zval_ptr_dtor_nogc(free_op1); HANDLE_EXCEPTION(); } } else { @@ -43764,6 +43769,7 @@ try_instanceof: ce = zend_fetch_class(NULL, opline->op2.num); if (UNEXPECTED(ce == NULL)) { ZEND_ASSERT(EG(exception)); + zval_ptr_dtor_nogc(free_op1); HANDLE_EXCEPTION(); } } else { -- 2.40.0