Skip to content

Use get_active_function_or_method_name() for zend_forbid_dynamic_call() #8762

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

Merged
merged 1 commit into from
Jun 16, 2022
Merged
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
5 changes: 4 additions & 1 deletion Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,10 @@ static zend_always_inline zend_result zend_forbid_dynamic_call(void)
ZEND_ASSERT(ex != NULL && ex->func != NULL);

if (ZEND_CALL_INFO(ex) & ZEND_CALL_DYNAMIC) {
zend_throw_error(NULL, "Cannot call %s() dynamically", get_active_function_name());
zend_string *function_or_method_name = get_active_function_or_method_name();
zend_throw_error(NULL, "Cannot call %.*s() dynamically",
(int) ZSTR_LEN(function_or_method_name), ZSTR_VAL(function_or_method_name));
zend_string_release(function_or_method_name);
return FAILURE;
}

Expand Down
17 changes: 17 additions & 0 deletions ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static zend_class_entry *zend_test_attribute;
static zend_class_entry *zend_test_parameter_attribute;
static zend_class_entry *zend_test_class_with_method_with_parameter_attribute;
static zend_class_entry *zend_test_child_class_with_method_with_parameter_attribute;
static zend_class_entry *zend_test_forbid_dynamic_call;
static zend_class_entry *zend_test_ns_foo_class;
static zend_class_entry *zend_test_ns2_foo_class;
static zend_class_entry *zend_test_ns2_ns_foo_class;
Expand Down Expand Up @@ -552,6 +553,20 @@ static ZEND_METHOD(ZendTestChildClassWithMethodWithParameterAttribute, override)
RETURN_LONG(4);
}

static ZEND_METHOD(ZendTestForbidDynamicCall, call)
{
ZEND_PARSE_PARAMETERS_NONE();

zend_forbid_dynamic_call();
}

static ZEND_METHOD(ZendTestForbidDynamicCall, callStatic)
{
ZEND_PARSE_PARAMETERS_NONE();

zend_forbid_dynamic_call();
}

PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("zend_test.replace_zend_execute_ex", "0", PHP_INI_SYSTEM, OnUpdateBool, replace_zend_execute_ex, zend_zend_test_globals, zend_test_globals)
STD_PHP_INI_BOOLEAN("zend_test.register_passes", "0", PHP_INI_SYSTEM, OnUpdateBool, register_passes, zend_zend_test_globals, zend_test_globals)
Expand Down Expand Up @@ -643,6 +658,8 @@ PHP_MINIT_FUNCTION(zend_test)
ZVAL_PSTRING(&attr->args[0].value, "value4");
}

zend_test_forbid_dynamic_call = register_class_ZendTestForbidDynamicCall();

zend_test_ns_foo_class = register_class_ZendTestNS_Foo();
zend_test_ns2_foo_class = register_class_ZendTestNS2_Foo();
zend_test_ns2_ns_foo_class = register_class_ZendTestNS2_ZendSubNS_Foo();
Expand Down
5 changes: 5 additions & 0 deletions ext/zend_test/test.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class ZendTestChildClassWithMethodWithParameterAttribute extends ZendTestClassWi
public function override(string $parameter): int {}
}

final class ZendTestForbidDynamicCall {
public function call(): void {}
public static function callStatic(): void {}
}

enum ZendTestUnitEnum {
case Foo;
case Bar;
Expand Down
26 changes: 25 additions & 1 deletion ext/zend_test/test_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions ext/zend_test/tests/zend_forbid_dynamic_call.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Zend: Test zend_forbid_dynamic_call() for methods
--EXTENSIONS--
zend_test
--FILE--
<?php

$object = new ZendTestForbidDynamicCall();
$object->call();
ZendTestForbidDynamicCall::callStatic();

try {
$call = [$object, 'call'];
$call();
} catch (Error $error) {
echo $error->getMessage(), "\n";
}

try {
$callStatic = [ZendTestForbidDynamicCall::class, 'callStatic'];
$callStatic();
} catch (Error $error) {
echo $error->getMessage(), "\n";
}

?>
--EXPECT--
Cannot call ZendTestForbidDynamicCall::call() dynamically
Cannot call ZendTestForbidDynamicCall::callStatic() dynamically