Skip to content

Commit 5b8cfc2

Browse files
committed
assert() special case
1 parent cec090f commit 5b8cfc2

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
PFA of assert() behaves like a dynamic call to assert()
3+
--FILE--
4+
<?php
5+
6+
try {
7+
echo "# Static call:\n";
8+
assert(false);
9+
} catch (Error $e) {
10+
echo $e::class, ": ", $e->getMessage(), "\n";
11+
}
12+
13+
try {
14+
echo "# Dynamic call:\n";
15+
(function ($f) { $f(false); })('assert');
16+
} catch (Error $e) {
17+
echo $e::class, ": ", $e->getMessage() ?: '(no message)', "\n";
18+
}
19+
20+
try {
21+
echo "# PFA call:\n";
22+
$f = assert(?);
23+
$f(false);
24+
} catch (Error $e) {
25+
echo $e::class, ": ", $e->getMessage() ?: '(no message)', "\n";
26+
}
27+
28+
?>
29+
--EXPECT--
30+
# Static call:
31+
AssertionError: assert(false)
32+
# Dynamic call:
33+
AssertionError: (no message)
34+
# PFA call:
35+
AssertionError: (no message)

Zend/zend_partial.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,18 @@ zend_ast *zp_compile_forwarding_call(
492492
bool forward_superfluous_args,
493493
zend_ast *stmts_ast)
494494
{
495+
bool is_assert = zend_string_equals(function->common.function_name,
496+
ZSTR_KNOWN(ZEND_STR_ASSERT));
497+
495498
/* Generate function body */
499+
496500
zend_ast *args_ast = zend_ast_create_list(0, ZEND_AST_ARG_LIST);
501+
502+
if (is_assert) {
503+
args_ast = zend_ast_list_add(args_ast,
504+
zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_ASSERT)));
505+
}
506+
497507
for (uint32_t offset = 0; offset < argc; offset++) {
498508
if (Z_ISUNDEF(argv[offset])) {
499509
/* Argument was not passed. Pass its default value. */
@@ -556,7 +566,11 @@ zend_ast *zp_compile_forwarding_call(
556566
}
557567

558568
zend_ast *call_ast;
559-
if (function->common.fn_flags & ZEND_ACC_CLOSURE) {
569+
if (is_assert) {
570+
zend_ast *func_name_ast = zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_CALL_USER_FUNC));
571+
func_name_ast->attr = ZEND_NAME_FQ;
572+
call_ast = zend_ast_create(ZEND_AST_CALL, func_name_ast, args_ast);
573+
} else if (function->common.fn_flags & ZEND_ACC_CLOSURE) {
560574
zend_ast *fn_ast = zend_ast_create(ZEND_AST_VAR,
561575
zend_ast_create_zval_from_str(zend_string_copy(param_names[argc + variadic_partial + (extra_named_params != NULL)])));
562576
call_ast = zend_ast_create(ZEND_AST_CALL, fn_ast, args_ast);

Zend/zend_string.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ EMPTY_SWITCH_DEFAULT_CASE()
635635
_(ZEND_STR_FUNC_GET_ARG, "func_get_arg") \
636636
_(ZEND_STR_COMPACT, "compact") \
637637
_(ZEND_STR_EXTRACT, "extract") \
638+
_(ZEND_STR_ASSERT, "assert") \
639+
_(ZEND_STR_CALL_USER_FUNC, "call_user_func") \
638640
_(ZEND_STR_ARRAY_SLICE, "array_slice") \
639641
_(ZEND_STR_SENSITIVEPARAMETER, "SensitiveParameter") \
640642
_(ZEND_STR_CONST_EXPR_PLACEHOLDER, "[constant expression]") \

0 commit comments

Comments
 (0)