Skip to content

Commit f9652f0

Browse files
committed
Assert zend_call_method instance call contract
Add a debug assertion to catch calls to instance methods without an object. Keep zend_test as a direct wrapper around the C API and pass objects to instance methods in the static return test. Refs #25405.
1 parent 3efb356 commit f9652f0

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎Zend/zend_interfaces.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ ZEND_API zval* zend_call_method(zend_object *object, zend_class_entry *obj_ce, z
7272
} else {
7373
fn = *fn_proxy;
7474
}
75+
ZEND_ASSERT(object || !fn->common.scope || (fn->common.fn_flags & ZEND_ACC_STATIC));
7576

7677
if (object) {
7778
called_scope = object->ce;

‎ext/zend_test/test.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,14 @@ static ZEND_FUNCTION(zend_call_method)
11271127
zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(class_or_object));
11281128
return;
11291129
}
1130+
if (!obj) {
1131+
zend_function *fn = zend_hash_str_find_ptr_lc(
1132+
&ce->function_table, ZSTR_VAL(method_name), ZSTR_LEN(method_name));
1133+
if (fn && fn->common.scope && !(fn->common.fn_flags & ZEND_ACC_STATIC)) {
1134+
zend_argument_value_error(1, "must be an object when calling an instance method");
1135+
return;
1136+
}
1137+
}
11301138

11311139
ZEND_ASSERT((argc >= 2) && (argc <= 4));
11321140
zend_call_method(obj, ce, NULL, ZSTR_VAL(method_name), ZSTR_LEN(method_name), return_value, argc - 2, arg1, arg2);

‎ext/zend_test/tests/internal-call-internal-static-return.phpt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var_dump(zend_call_method(IntIntStaticString::class, "tryFrom", "all the fish"))
2020

2121
class StillReturnsStatic extends _ZendTestClass {}
2222

23-
var_dump(get_class(zend_call_method(_ZendTestClass::class, "returnsStatic")));
24-
var_dump(get_class(zend_call_method(StillReturnsStatic::class, "returnsStatic")));
23+
var_dump(get_class(zend_call_method(new _ZendTestClass(), "returnsStatic")));
24+
var_dump(get_class(zend_call_method(new StillReturnsStatic(), "returnsStatic")));
2525

2626
?>
2727
--EXPECT--
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
zend_call_method() rejects instance methods called without an object
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
class Demo {
8+
public mixed $value = true;
9+
10+
public function returnTrue() {
11+
var_dump($this);
12+
return true;
13+
}
14+
15+
public function usesThis() {
16+
return $this !== null;
17+
}
18+
19+
public function &returnsRef() {
20+
return $this->value;
21+
}
22+
23+
public function returnsProp() {
24+
return $this->value;
25+
}
26+
27+
public function callsMethod() {
28+
return $this->method();
29+
}
30+
31+
public function method() {
32+
return 5;
33+
}
34+
35+
public static function staticMethod() {
36+
return 'static';
37+
}
38+
}
39+
40+
foreach (['returnTrue', 'returnsRef', 'returnsProp', 'callsMethod'] as $method) {
41+
try {
42+
zend_call_method(Demo::class, $method);
43+
} catch (ValueError $e) {
44+
echo $e::class, "\n";
45+
}
46+
}
47+
48+
var_dump(zend_call_method(new Demo(), 'usesThis'));
49+
var_dump(zend_call_method(new Demo(), 'returnsProp'));
50+
var_dump(zend_call_method(new Demo(), 'callsMethod'));
51+
var_dump(zend_call_method(Demo::class, 'staticMethod'));
52+
?>
53+
--EXPECT--
54+
ValueError
55+
ValueError
56+
ValueError
57+
ValueError
58+
bool(true)
59+
bool(true)
60+
int(5)
61+
string(6) "static"

0 commit comments

Comments
 (0)