Skip to content

Commit 5716fa7

Browse files
committed
Make Exception::$previous a typed property
Exception::$previous is a private property, so we can add a type: private ?Throwable $previous = null;
1 parent aaae77f commit 5716fa7

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

Zend/tests/bug70121.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ Bug #70121 (unserialize() could lead to unexpected methods execution / NULL poin
55
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:{}}}');
66
?>
77
OK
8-
--EXPECT--
9-
OK
8+
--EXPECTF--
9+
Fatal error: Uncaught TypeError: Cannot assign stdClass to property Exception::$previous of type ?Throwable in %s:%d
10+
Stack trace:
11+
#0 %s(%d): unserialize('O:12:"DateInter...')
12+
#1 {main}
13+
thrown in %s on line %d

Zend/zend_exceptions.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,7 @@ ZEND_METHOD(Exception, __wakeup)
316316
CHECK_EXC_TYPE(ZEND_STR_CODE, IS_LONG);
317317
CHECK_EXC_TYPE(ZEND_STR_FILE, IS_STRING);
318318
CHECK_EXC_TYPE(ZEND_STR_LINE, IS_LONG);
319-
CHECK_EXC_TYPE(ZEND_STR_TRACE, IS_ARRAY);
320-
pvalue = zend_read_property(i_get_exception_base(object), object, "previous", sizeof("previous")-1, 1, &value);
321-
if (pvalue && Z_TYPE_P(pvalue) != IS_NULL && (Z_TYPE_P(pvalue) != IS_OBJECT ||
322-
!instanceof_function(Z_OBJCE_P(pvalue), zend_ce_throwable) ||
323-
pvalue == object)) {
324-
zend_unset_property(i_get_exception_base(object), object, "previous", sizeof("previous")-1);
325-
}
319+
/* The type of $trace and $previous is enforced through typed properties. */
326320
}
327321
/* }}} */
328322

@@ -751,7 +745,10 @@ static void declare_exception_properties(zend_class_entry *ce)
751745
ce, ZSTR_KNOWN(ZEND_STR_TRACE), &val, ZEND_ACC_PRIVATE, NULL,
752746
(zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ARRAY));
753747

754-
zend_declare_property_null(ce, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE);
748+
ZVAL_NULL(&val);
749+
zend_declare_typed_property(
750+
ce, ZSTR_KNOWN(ZEND_STR_PREVIOUS), &val, ZEND_ACC_PRIVATE, NULL,
751+
(zend_type) ZEND_TYPE_INIT_CE(zend_ce_throwable, /* allow_null */ 1, 0));
755752
}
756753

757754
void zend_register_default_exception(void) /* {{{ */

ext/standard/tests/serialize/bug69793.phpt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ $e = unserialize('O:9:"Exception":7:{s:17:"'."\0".'Exception'."\0".'string";s:1:
77
var_dump($e."");
88
?>
99
--EXPECTF--
10-
Warning: Undefined property: Exception::$file in %s on line %d
11-
12-
Warning: Undefined property: Exception::$previous in %s on line %d
13-
14-
Warning: Undefined property: Exception::$previous in %s on line %d
15-
string(41) "Exception in :1337
10+
Fatal error: Uncaught TypeError: Cannot assign int to property Exception::$previous of type ?Throwable in %s:%d
1611
Stack trace:
17-
#0 {main}"
12+
#0 %s(%d): unserialize('O:9:"Exception"...')
13+
#1 {main}
14+
thrown in %s on line %d

sapi/cli/tests/005.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ string(183) "Class [ <internal:Core> class stdClass ] {
3737
}
3838

3939
"
40-
string(2166) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {
40+
string(2177) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {
4141

4242
- Constants [0] {
4343
}
@@ -55,7 +55,7 @@ string(2166) "Class [ <internal:Core> class Exception implements Throwable, Stri
5555
Property [ protected $file = NULL ]
5656
Property [ protected $line = NULL ]
5757
Property [ private array $trace = Array ]
58-
Property [ private $previous = NULL ]
58+
Property [ private ?Throwable $previous = NULL ]
5959
}
6060

6161
- Methods [11] {

0 commit comments

Comments
 (0)