From: Nikita Popov Date: Thu, 28 May 2020 12:01:29 +0000 (+0200) Subject: Make Exception::$previous a typed property X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5716fa7f49db138f9258ef5f33105f9ee4a4c772;p=php Make Exception::$previous a typed property Exception::$previous is a private property, so we can add a type: private ?Throwable $previous = null; --- diff --git a/Zend/tests/bug70121.phpt b/Zend/tests/bug70121.phpt index 9f8b7d6055..5b4aaa3a76 100644 --- a/Zend/tests/bug70121.phpt +++ b/Zend/tests/bug70121.phpt @@ -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 diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c index 21775a26ca..6262685c47 100644 --- a/Zend/zend_exceptions.c +++ b/Zend/zend_exceptions.c @@ -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) /* {{{ */ diff --git a/ext/standard/tests/serialize/bug69793.phpt b/ext/standard/tests/serialize/bug69793.phpt index b2d9fbb256..6c2d1df026 100644 --- a/ext/standard/tests/serialize/bug69793.phpt +++ b/ext/standard/tests/serialize/bug69793.phpt @@ -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 diff --git a/sapi/cli/tests/005.phpt b/sapi/cli/tests/005.phpt index c037889c41..ab616d779f 100644 --- a/sapi/cli/tests/005.phpt +++ b/sapi/cli/tests/005.phpt @@ -37,7 +37,7 @@ string(183) "Class [ class stdClass ] { } " -string(2166) "Class [ class Exception implements Throwable, Stringable ] { +string(2177) "Class [ class Exception implements Throwable, Stringable ] { - Constants [0] { } @@ -55,7 +55,7 @@ string(2166) "Class [ 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] {