com php-src: fix foreach, possibly ...: Zend/tests/type_decla rations/typed_properties_036.phpt Zend/zend_exe cute.c Zend/zend_execute.h
Zend/zend_vm_def.h Zend/zend_vm_execute.h
| From: | Bob Weinand | Date: | Wed, 30 Mar 2016 13:59:08 +0000 |
| Subject: | com php-src: fix foreach, possibly ...: Zend/tests/type_decla rations/typed_properties_036.phpt Zend/zend_exe cute.c Zend/zend_execute.h Zend/zend_vm_def.h Zend/zend_vm_execute.h |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-95649@lists.php.net to get a copy of this message | ||
Commit: efe506a9de5695fd684dc733b91002d6bb9cb613
Author: Joe Watkins <krakjoe@php.net> Wed, 30 Mar 2016 14:59:08 +0100
Parents: e48ecd83afc3e300c67ced8343a97a2167a5e7b2
Branches: typed_ref_properties
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=efe506a9de5695fd684dc733b91002d6bb9cb613
Log:
fix foreach, possibly ...
Changed paths:
A Zend/tests/type_declarations/typed_properties_036.phpt
M Zend/zend_execute.c
M Zend/zend_execute.h
M Zend/zend_vm_def.h
M Zend/zend_vm_execute.h
Diff:
diff --git a/Zend/tests/type_declarations/typed_properties_036.phpt
b/Zend/tests/type_declarations/typed_properties_036.phpt
new file mode 100644
index 0000000..5ae25d6
--- /dev/null
+++ b/Zend/tests/type_declarations/typed_properties_036.phpt
@@ -0,0 +1,20 @@
+--TEST--
+Test typed properties normal foreach must not yield null
+--FILE--
+<?php
+$foo = new class {
+ public int $bar = 10,
+ int $qux;
+};
+
+foreach ($foo as $key => $bar)
+ var_dump($key, $bar);
+--EXPECTF--
+string(3) "bar"
+int(10)
+
+Fatal error: Uncaught TypeError: Typed property class@anonymous::$qux must not be accessed before
initialization in %s:7
+Stack trace:
+#0 {main}
+ thrown in %s on line 7
+
diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c
index 24fbd3f..70eca49 100644
--- a/Zend/zend_execute.c
+++ b/Zend/zend_execute.c
@@ -787,6 +787,24 @@ static int zend_verify_internal_arg_type(zend_function *zf, uint32_t arg_num,
zv
return 1;
}
+zend_property_info* zend_object_fetch_property_type_info_ex(zval *object, zend_string *property,
void **cache_slot) {
+ zend_property_info *info = NULL;
+
+ if (cache_slot && EXPECTED(Z_OBJCE_P(object) == CACHED_PTR_EX(cache_slot))) {
+ zend_property_info *cached = (zend_property_info*)CACHED_PTR_EX(cache_slot + 2);
+
+ return (cached && cached->type) ? cached : NULL;
+ }
+
+ info = zend_get_property_info(Z_OBJCE_P(object), property, 1);
+
+ if (UNEXPECTED(info && info != ZEND_WRONG_PROPERTY_INFO && info->type)) {
+ return info;
+ }
+
+ return NULL;
+}
+
zend_property_info* zend_object_fetch_property_type_info(zval *object, zval *property, void
**cache_slot)
{
if (cache_slot && EXPECTED(Z_OBJCE_P(object) == CACHED_PTR_EX(cache_slot))) {
@@ -794,6 +812,7 @@ zend_property_info* zend_object_fetch_property_type_info(zval *object, zval *pro
return (info && info->type) ? info : NULL;
}
+
do {
if (UNEXPECTED(Z_TYPE_P(property) != IS_STRING)) {
break;
@@ -809,13 +828,7 @@ zend_property_info* zend_object_fetch_property_type_info(zval *object, zval
*pro
}
}
- if (EXPECTED(Z_TYPE_P(property) == IS_STRING)) {
- zend_property_info *info = zend_get_property_info(Z_OBJCE_P(object), Z_STR_P(property), 1);
-
- if (UNEXPECTED(info && info != ZEND_WRONG_PROPERTY_INFO && info->type)) {
- return info;
- }
- }
+ return zend_object_fetch_property_type_info_ex(object, Z_STR_P(property), cache_slot);
} while (0);
return NULL;
diff --git a/Zend/zend_execute.h b/Zend/zend_execute.h
index e5c136c..28889c3 100644
--- a/Zend/zend_execute.h
+++ b/Zend/zend_execute.h
@@ -383,6 +383,7 @@ static zend_always_inline zend_bool _zend_object_has_type_hints(zval *object) {
#define ZEND_OBJECT_HAS_TYPE_HINTS(object) _zend_object_has_type_hints(object)
zend_property_info* zend_object_fetch_property_type_info(zval *object, zval *property, void
**cache);
+zend_property_info* zend_object_fetch_property_type_info_ex(zval *object, zend_string *property,
void **cache);
zend_bool zend_verify_property_type(zend_property_info *info, zval *property, zend_bool strict);
void zend_verify_property_type_error(zend_property_info *info, zend_string *name, zval *property);
diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h
index 0e45b8f..51a1e83 100644
--- a/Zend/zend_vm_def.h
+++ b/Zend/zend_vm_def.h
@@ -6459,6 +6459,21 @@ ZEND_VM_HANDLER(78, ZEND_FE_FETCH_R, VAR, ANY, JMP_ADDR)
pos++;
p++;
}
+
+ if (UNEXPECTED(ZEND_OBJECT_HAS_TYPE_HINTS(array) && p->key)) {
+ zend_property_info *prop_info = zend_object_fetch_property_type_info_ex(array, p->key,
NULL);
+
+ if (UNEXPECTED(prop_info)) {
+ if (UNEXPECTED(Z_TYPE_P(value) == IS_NULL)) {
+ zend_throw_exception_ex(zend_ce_type_error, prop_info->type,
+ "Typed property %s::$%s must not be accessed before initialization",
+ ZSTR_VAL(prop_info->ce->name),
+ ZSTR_VAL(p->key));
+ HANDLE_EXCEPTION();
+ }
+ }
+ }
+
if (opline->result_type & (IS_TMP_VAR|IS_CV)) {
if (UNEXPECTED(!p->key)) {
ZVAL_LONG(EX_VAR(opline->result.var), p->h);
diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h
index c44587c..c4270b0 100644
--- a/Zend/zend_vm_execute.h
+++ b/Zend/zend_vm_execute.h
@@ -17254,6 +17254,21 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL
ZEND_FE_FETCH_R_SPEC_VAR_HANDLER(ZE
pos++;
p++;
}
+
+ if (UNEXPECTED(ZEND_OBJECT_HAS_TYPE_HINTS(array) && p->key)) {
+ zend_property_info *prop_info = zend_object_fetch_property_type_info_ex(array, p->key,
NULL);
+
+ if (UNEXPECTED(prop_info)) {
+ if (UNEXPECTED(Z_TYPE_P(value) == IS_NULL)) {
+ zend_throw_exception_ex(zend_ce_type_error, prop_info->type,
+ "Typed property %s::$%s must not be accessed before initialization",
+ ZSTR_VAL(prop_info->ce->name),
+ ZSTR_VAL(p->key));
+ HANDLE_EXCEPTION();
+ }
+ }
+ }
+
if (opline->result_type & (IS_TMP_VAR|IS_CV)) {
if (UNEXPECTED(!p->key)) {
ZVAL_LONG(EX_VAR(opline->result.var), p->h);