Skip to content

Commit d81b1b8

Browse files
committed
review
1 parent 3c616f5 commit d81b1b8

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

Zend/tests/constexpr/constant_expressions_cast.phpt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ const T5 = (float) 5;
1414
const T6 = (array) "";
1515
const T7 = (array) var_dump(...);
1616
const T8 = (array) new X;
17+
const T9 = (array) new DateTime;
18+
const T10 = (int) new DateTime;
1719

18-
var_dump(T1, T2, T3, T4, T5, T6, T7, T8);
20+
var_dump(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
1921
?>
2022
--EXPECTF--
2123
Warning: Array to string conversion in %s on line %d
24+
25+
Warning: Object of class DateTime could not be converted to int in %s on line %d
2226
int(0)
2327
bool(true)
2428
string(5) "Array"
@@ -49,3 +53,12 @@ array(1) {
4953
["foo"]=>
5054
int(3)
5155
}
56+
array(3) {
57+
["date"]=>
58+
string(%d) "%s"
59+
["timezone_type"]=>
60+
int(%d)
61+
["timezone"]=>
62+
string(%d) "%s"
63+
}
64+
int(1)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
Constant expressions with object cast in property
3+
--FILE--
4+
<?php
5+
class X {
6+
public $foo = (object) [];
7+
}
8+
?>
9+
--EXPECTF--
10+
Fatal error: Object casts are not supported in this context in %s on line %d

Zend/zend_ast.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -736,14 +736,21 @@ ZEND_API zend_result ZEND_FASTCALL zend_ast_evaluate_inner(
736736
} else {
737737
ZVAL_EMPTY_ARRAY(result);
738738
}
739-
} else {
740-
/* Constant AST only allows building stdClass objects, closures, or user classes;
741-
* all of which should be compatible. */
742-
ZEND_ASSERT(ZEND_STD_BUILD_OBJECT_PROPERTIES_ARRAY_COMPATIBLE(&op1));
743-
744-
/* Optimized version without rebuilding properties HashTable */
739+
} else if (ZEND_STD_BUILD_OBJECT_PROPERTIES_ARRAY_COMPATIBLE(&op1)) {
745740
ZVAL_ARR(result, zend_std_build_object_properties_array(Z_OBJ(op1)));
746741
zval_ptr_dtor_nogc(&op1);
742+
} else {
743+
HashTable *obj_ht = zend_get_properties_for(&op1, ZEND_PROP_PURPOSE_ARRAY_CAST);
744+
if (obj_ht) {
745+
ZVAL_ARR(result, zend_proptable_to_symtable(obj_ht,
746+
(Z_OBJCE(op1)->default_properties_count ||
747+
Z_OBJ(op1)->handlers != &std_object_handlers ||
748+
GC_IS_RECURSIVE(obj_ht))));
749+
zend_release_properties(obj_ht);
750+
} else {
751+
ZVAL_EMPTY_ARRAY(result);
752+
}
753+
zval_ptr_dtor_nogc(&op1);
747754
}
748755
break;
749756
case IS_OBJECT:

Zend/zend_compile.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11431,6 +11431,12 @@ static void zend_compile_const_expr(zend_ast **ast_ptr, void *context) /* {{{ */
1143111431
case ZEND_AST_MAGIC_CONST:
1143211432
zend_compile_const_expr_magic_const(ast_ptr);
1143311433
break;
11434+
case ZEND_AST_CAST:
11435+
if (ast->attr == IS_OBJECT && !ctx->allow_dynamic) {
11436+
zend_error_noreturn(E_COMPILE_ERROR,
11437+
"Object casts are not supported in this context");
11438+
}
11439+
break;
1143411440
case ZEND_AST_NEW:
1143511441
if (!ctx->allow_dynamic) {
1143611442
zend_error_noreturn(E_COMPILE_ERROR,

0 commit comments

Comments
 (0)