]> granicus.if.org Git - php/commitdiff
Make Exception::$previous a typed property
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 28 May 2020 12:01:29 +0000 (14:01 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 28 May 2020 12:01:29 +0000 (14:01 +0200)
Exception::$previous is a private property, so we can add a type:

    private ?Throwable $previous = null;

Zend/tests/bug70121.phpt
Zend/zend_exceptions.c
ext/standard/tests/serialize/bug69793.phpt
sapi/cli/tests/005.phpt

index 9f8b7d6055db35858ec0db5a7040974a5f8cf16b..5b4aaa3a7679be826aa120d78cb52c29ad917986 100644 (file)
@@ -5,5 +5,9 @@ Bug #70121 (unserialize() could lead to unexpected methods execution / NULL poin
 unserialize('O:12:"DateInterval":1:{s:4:"days";O:9:"Exception":7:{s:10:"'."\0".'*'."\0".'message";s:1:"x";s:17:"'."\0".'Exception'."\0".'string";s:1:"A";s:7:"'."\0".'*'."\0".'code";i:0;s:7:"'."\0".'*'."\0".'file";s:1:"a";s:7:"'."\0".'*'."\0".'line";i:1337;s:16:"'."\0".'Exception'."\0".'trace";a:0:{}s:19:"'."\0".'Exception'."\0".'previous";O:8:"stdClass":0:{}}}');
 ?>
 OK
---EXPECT--
-OK
+--EXPECTF--
+Fatal error: Uncaught TypeError: Cannot assign stdClass to property Exception::$previous of type ?Throwable in %s:%d
+Stack trace:
+#0 %s(%d): unserialize('O:12:"DateInter...')
+#1 {main}
+  thrown in %s on line %d
index 21775a26ca23135878619e2097d8ea9b11b7d68c..6262685c47d6fe20f2a04a611c727563394b2ef5 100644 (file)
@@ -316,13 +316,7 @@ ZEND_METHOD(Exception, __wakeup)
        CHECK_EXC_TYPE(ZEND_STR_CODE,     IS_LONG);
        CHECK_EXC_TYPE(ZEND_STR_FILE,     IS_STRING);
        CHECK_EXC_TYPE(ZEND_STR_LINE,     IS_LONG);
-       CHECK_EXC_TYPE(ZEND_STR_TRACE,    IS_ARRAY);
-       pvalue = zend_read_property(i_get_exception_base(object), object, "previous", sizeof("previous")-1, 1, &value);
-       if (pvalue && Z_TYPE_P(pvalue) != IS_NULL && (Z_TYPE_P(pvalue) != IS_OBJECT ||
-                       !instanceof_function(Z_OBJCE_P(pvalue), zend_ce_throwable) ||
-                       pvalue == object)) {
-               zend_unset_property(i_get_exception_base(object), object, "previous", sizeof("previous")-1);
-       }
+       /* The type of $trace and $previous is enforced through typed properties. */
 }
 /* }}} */
 
@@ -751,7 +745,10 @@ static void declare_exception_properties(zend_class_entry *ce)
                ce, ZSTR_KNOWN(ZEND_STR_TRACE), &val, ZEND_ACC_PRIVATE, NULL,
                (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY));
 
-       zend_declare_property_null(ce, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE);
+       ZVAL_NULL(&val);
+       zend_declare_typed_property(
+               ce, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &val, ZEND_ACC_PRIVATE, NULL,
+               (zend_type) ZEND_TYPE_INIT_CE(zend_ce_throwable, /* allow_null */ 1, 0));
 }
 
 void zend_register_default_exception(void) /* {{{ */
index b2d9fbb2566f2a6cd4ae71357e79e50b85fb706c..6c2d1df026256b9d384f4431d66c38ec30a1c6fd 100644 (file)
@@ -7,11 +7,8 @@ $e = unserialize('O:9:"Exception":7:{s:17:"'."\0".'Exception'."\0".'string";s:1:
 var_dump($e."");
 ?>
 --EXPECTF--
-Warning: Undefined property: Exception::$file in %s on line %d
-
-Warning: Undefined property: Exception::$previous in %s on line %d
-
-Warning: Undefined property: Exception::$previous in %s on line %d
-string(41) "Exception in :1337
+Fatal error: Uncaught TypeError: Cannot assign int to property Exception::$previous of type ?Throwable in %s:%d
 Stack trace:
-#0 {main}"
+#0 %s(%d): unserialize('O:9:"Exception"...')
+#1 {main}
+  thrown in %s on line %d
index c037889c41c5e741cc45c8a0d958e00493c8e2c1..ab616d779f828c2af93b9b727c0175c42c978a9e 100644 (file)
@@ -37,7 +37,7 @@ string(183) "Class [ <internal:Core> class stdClass ] {
 }
 
 "
-string(2166) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {
+string(2177) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {
 
   - Constants [0] {
   }
@@ -55,7 +55,7 @@ string(2166) "Class [ <internal:Core> class Exception implements Throwable, Stri
     Property [ protected $file = NULL ]
     Property [ protected $line = NULL ]
     Property [ private array $trace = Array ]
-    Property [ private $previous = NULL ]
+    Property [ private ?Throwable $previous = NULL ]
   }
 
   - Methods [11] {