Make Exception::$previous a typed property

Exception::$previous is a private property, so we can add a type:

    private ?Throwable $previous = null;
This commit is contained in:
Nikita Popov 2020-05-28 14:01:29 +02:00
parent aaae77f7f1
commit 5716fa7f49
4 changed files with 17 additions and 19 deletions

View 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

View 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) /* {{{ */

View 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

View 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] {