Skip to content

Commit 30a082c

Browse files
committed
Make some exception properties typed
Closes phpGH-6891
1 parent 31b6f84 commit 30a082c

17 files changed

+49
-54
lines changed

Zend/tests/bug50005.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Bug #50005 (Throwing through Reflection modified Exception object makes segmenta
55

66
class a extends exception {
77
public function __construct() {
8-
$this->file = null;
8+
$this->file = "";
99
}
1010
}
1111

Zend/tests/bug64821.1.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class a extends exception {
88
$this->message = NULL;
99
$this->string = NULL;
1010
$this->code = array();
11-
$this->line = "hello";
11+
$this->line = 0;
1212
}
1313
}
1414

Zend/tests/bug64821.2.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Bug #64821 Custom Exceptions crash when internal properties overridden (variatio
55

66
class a extends exception {
77
public function __construct() {
8-
$this->line = array();
8+
$this->line = 0;
99
}
1010
}
1111

Zend/tests/bug64821.3.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Bug #64821 Custom Exceptions crash when internal properties overridden (variatio
55

66
class a extends exception {
77
public function __construct() {
8-
$this->line = array();
9-
$this->file = NULL;
8+
$this->line = 0;
9+
$this->file = "";
1010
}
1111
}
1212

Zend/zend_exceptions.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,11 @@ void zend_register_default_exception(void) /* {{{ */
757757

758758
zend_ce_error_exception = register_class_ErrorException(zend_ce_exception);
759759
zend_ce_error_exception->create_object = zend_error_exception_new;
760+
760761
/* Declared manually because it uses constant E_ERROR. */
761-
zend_declare_property_long(zend_ce_error_exception, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED);
762+
zval severity_default_value;
763+
ZVAL_LONG(&severity_default_value, E_ERROR);
764+
zend_declare_typed_property(zend_ce_error_exception, ZSTR_KNOWN(ZEND_STR_SEVERITY), &severity_default_value, ZEND_ACC_PROTECTED, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
762765

763766
zend_ce_error = register_class_Error(zend_ce_throwable);
764767
zend_ce_error->create_object = zend_default_exception_new;

Zend/zend_exceptions.stub.php

+11-18
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@ public function getTraceAsString(): string;
2222

2323
class Exception implements Throwable
2424
{
25-
/** @var string */
25+
/** @var string Intentionally left untyped for BC reasons */
2626
protected $message = "";
27-
/** @var string */
28-
private $string = "";
29-
/** @var int */
27+
private string $string = "";
28+
/** @var int Intentionally left untyped for BC reasons */
3029
protected $code = 0;
31-
/** @var string|null */
32-
protected $file = null;
33-
/** @var int|null */
34-
protected $line = null;
30+
protected string $file = "";
31+
protected int $line = 0;
3532
private array $trace = [];
3633
private ?Throwable $previous = null;
3734

@@ -62,8 +59,7 @@ public function __toString(): string {}
6259

6360
class ErrorException extends Exception
6461
{
65-
/** @var int */
66-
protected $severity = E_ERROR;
62+
protected int $severity = E_ERROR;
6763

6864
public function __construct(
6965
string $message = "",
@@ -79,16 +75,13 @@ final public function getSeverity(): int {}
7975

8076
class Error implements Throwable
8177
{
82-
/** @var string */
78+
/** @var string Intentionally left untyped for BC reasons */
8379
protected $message = "";
84-
/** @var string */
85-
private $string = "";
86-
/** @var int */
80+
private string $string = "";
81+
/** @var int Intentionally left untyped for BC reasons */
8782
protected $code = 0;
88-
/** @var string|null */
89-
protected $file = null;
90-
/** @var int|null */
91-
protected $line = null;
83+
protected string $file = "";
84+
protected int $line;
9285
private array $trace = [];
9386
private ?Throwable $previous = null;
9487

Zend/zend_exceptions_arginfo.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: ef8c275a543d67fd96a775b0b18098ccdb428285 */
2+
* Stub hash: 9a9ce2975a7449a621d364beca646525fc56b294 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Throwable_getMessage, 0, 0, IS_STRING, 0)
55
ZEND_END_ARG_INFO()
@@ -214,7 +214,7 @@ static zend_class_entry *register_class_Exception(zend_class_entry *class_entry_
214214
zval property_string_default_value;
215215
ZVAL_EMPTY_STRING(&property_string_default_value);
216216
zend_string *property_string_name = zend_string_init("string", sizeof("string") - 1, 1);
217-
zend_declare_property_ex(class_entry, property_string_name, &property_string_default_value, ZEND_ACC_PRIVATE, NULL);
217+
zend_declare_typed_property(class_entry, property_string_name, &property_string_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
218218
zend_string_release(property_string_name);
219219

220220
zval property_code_default_value;
@@ -224,15 +224,15 @@ static zend_class_entry *register_class_Exception(zend_class_entry *class_entry_
224224
zend_string_release(property_code_name);
225225

226226
zval property_file_default_value;
227-
ZVAL_NULL(&property_file_default_value);
227+
ZVAL_EMPTY_STRING(&property_file_default_value);
228228
zend_string *property_file_name = zend_string_init("file", sizeof("file") - 1, 1);
229-
zend_declare_property_ex(class_entry, property_file_name, &property_file_default_value, ZEND_ACC_PROTECTED, NULL);
229+
zend_declare_typed_property(class_entry, property_file_name, &property_file_default_value, ZEND_ACC_PROTECTED, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
230230
zend_string_release(property_file_name);
231231

232232
zval property_line_default_value;
233-
ZVAL_NULL(&property_line_default_value);
233+
ZVAL_LONG(&property_line_default_value, 0);
234234
zend_string *property_line_name = zend_string_init("line", sizeof("line") - 1, 1);
235-
zend_declare_property_ex(class_entry, property_line_name, &property_line_default_value, ZEND_ACC_PROTECTED, NULL);
235+
zend_declare_typed_property(class_entry, property_line_name, &property_line_default_value, ZEND_ACC_PROTECTED, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
236236
zend_string_release(property_line_name);
237237

238238
zval property_trace_default_value;
@@ -278,7 +278,7 @@ static zend_class_entry *register_class_Error(zend_class_entry *class_entry_Thro
278278
zval property_string_default_value;
279279
ZVAL_EMPTY_STRING(&property_string_default_value);
280280
zend_string *property_string_name = zend_string_init("string", sizeof("string") - 1, 1);
281-
zend_declare_property_ex(class_entry, property_string_name, &property_string_default_value, ZEND_ACC_PRIVATE, NULL);
281+
zend_declare_typed_property(class_entry, property_string_name, &property_string_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
282282
zend_string_release(property_string_name);
283283

284284
zval property_code_default_value;
@@ -288,15 +288,15 @@ static zend_class_entry *register_class_Error(zend_class_entry *class_entry_Thro
288288
zend_string_release(property_code_name);
289289

290290
zval property_file_default_value;
291-
ZVAL_NULL(&property_file_default_value);
291+
ZVAL_EMPTY_STRING(&property_file_default_value);
292292
zend_string *property_file_name = zend_string_init("file", sizeof("file") - 1, 1);
293-
zend_declare_property_ex(class_entry, property_file_name, &property_file_default_value, ZEND_ACC_PROTECTED, NULL);
293+
zend_declare_typed_property(class_entry, property_file_name, &property_file_default_value, ZEND_ACC_PROTECTED, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
294294
zend_string_release(property_file_name);
295295

296296
zval property_line_default_value;
297-
ZVAL_NULL(&property_line_default_value);
297+
ZVAL_UNDEF(&property_line_default_value);
298298
zend_string *property_line_name = zend_string_init("line", sizeof("line") - 1, 1);
299-
zend_declare_property_ex(class_entry, property_line_name, &property_line_default_value, ZEND_ACC_PROTECTED, NULL);
299+
zend_declare_typed_property(class_entry, property_line_name, &property_line_default_value, ZEND_ACC_PROTECTED, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
300300
zend_string_release(property_line_name);
301301

302302
zval property_trace_default_value;

ext/mysqli/mysqli.stub.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,7 @@ public function next(): bool {}
599599

600600
final class mysqli_sql_exception extends RuntimeException
601601
{
602-
/** @var string */
603-
protected $sqlstate = "00000";
602+
protected string $sqlstate = "00000";
604603
}
605604

606605
function mysqli_affected_rows(mysqli $mysql): int|string {}

ext/mysqli/mysqli_arginfo.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 14c5c4960e70f5b38fc6c4f4403804cc28147b0b */
2+
* Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING)
55
ZEND_ARG_OBJ_INFO(0, mysql, mysqli, 0)
@@ -1356,7 +1356,7 @@ static zend_class_entry *register_class_mysqli_sql_exception(zend_class_entry *c
13561356
zend_string *property_sqlstate_default_value_str = zend_string_init("00000", sizeof("00000") - 1, 1);
13571357
ZVAL_STR(&property_sqlstate_default_value, property_sqlstate_default_value_str);
13581358
zend_string *property_sqlstate_name = zend_string_init("sqlstate", sizeof("sqlstate") - 1, 1);
1359-
zend_declare_property_ex(class_entry, property_sqlstate_name, &property_sqlstate_default_value, ZEND_ACC_PROTECTED, NULL);
1359+
zend_declare_typed_property(class_entry, property_sqlstate_name, &property_sqlstate_default_value, ZEND_ACC_PROTECTED, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
13601360
zend_string_release(property_sqlstate_name);
13611361

13621362
return class_entry;

ext/soap/tests/bug73452.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ soap
55
--FILE--
66
<?php
77

8-
$data = 'O:9:"SoapFault":4:{s:9:"faultcode";i:4298448493;s:11:"faultstring";i:4298448543;s:7:"'."\0*\0".'file";i:4298447319;s:7:"'."\0*\0".'line";s:4:"ryat";}';
8+
$data = 'O:9:"SoapFault":4:{s:9:"faultcode";i:4298448493;s:11:"faultstring";i:4298448543;s:7:"'."\0*\0".'file";s:0:"";s:7:"'."\0*\0".'line";i:0;}';
99
echo unserialize($data);
1010

1111
?>

ext/standard/tests/serialize/bug69793.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Bug #69793: Remotely triggerable stack exhaustion via recursive method calls
33
--FILE--
44
<?php
5-
$e = unserialize('O:9:"Exception":7:{s:17:"'."\0".'Exception'."\0".'string";s:1:"a";s:7:"'."\0".'*'."\0".'code";i:0;s:7:"'."\0".'*'."\0".'file";R:1;s:7:"'."\0".'*'."\0".'line";i:1337;s:16:"'."\0".'Exception'."\0".'trace";a:0:{}s:19:"'."\0".'Exception'."\0".'previous";i:10;s:10:"'."\0".'*'."\0".'message";N;}');
5+
$e = unserialize('O:9:"Exception":7:{s:17:"'."\0".'Exception'."\0".'string";s:1:"a";s:7:"'."\0".'*'."\0".'code";i:0;s:7:"'."\0".'*'."\0".'file";s:0:"";s:7:"'."\0".'*'."\0".'line";i:1337;s:16:"'."\0".'Exception'."\0".'trace";a:0:{}s:19:"'."\0".'Exception'."\0".'previous";i:10;s:10:"'."\0".'*'."\0".'message";N;}');
66

77
var_dump($e."");
88
?>

ext/standard/tests/serialize/bug72663_2.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ class obj implements Serializable {
1313
}
1414
}
1515

16-
$inner = 'a:1:{i:0;O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";R:4;}';
16+
$inner = 'a:1:{i:0;O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";s:0:"";}';
1717
$exploit = 'a:2:{i:0;C:3:"obj":'.strlen($inner).':{'.$inner.'}i:1;R:4;}';
1818
var_dump(unserialize($exploit));
1919

2020
?>
2121
--EXPECTF--
2222
Notice: unserialize(): Unexpected end of serialized data in %s on line %d
2323

24-
Notice: unserialize(): Error at offset 46 of 47 bytes in %s on line %d
24+
Notice: unserialize(): Error at offset 49 of 50 bytes in %s on line %d
2525

26-
Notice: unserialize(): Error at offset 79 of 80 bytes in %s on line %d
26+
Notice: unserialize(): Error at offset 82 of 83 bytes in %s on line %d
2727
bool(false)

ext/standard/tests/serialize/bug72663_3.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ session.serialize_handler=php_serialize
77
--FILE--
88
<?php
99
session_start();
10-
$sess = 'O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";R:1;}';
10+
$sess = 'O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";s:0:"";}';
1111
session_decode($sess);
1212
var_dump($_SESSION);
1313
?>

ext/standard/tests/strings/bug72663.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class obj implements Serializable {
1212
}
1313
}
1414

15-
$inner = 'a:1:{i:0;O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";R:4;}';
15+
$inner = 'a:1:{i:0;O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";s:0:"";}';
1616
$exploit = 'a:2:{i:0;C:3:"obj":'.strlen($inner).':{'.$inner.'}i:1;R:4;}';
1717

1818
var_dump(unserialize($exploit));
@@ -21,8 +21,8 @@ DONE
2121
--EXPECTF--
2222
Notice: unserialize(): Unexpected end of serialized data in %sbug72663.php on line %d
2323

24-
Notice: unserialize(): Error at offset 46 of 47 bytes in %sbug72663.php on line %d
24+
Notice: unserialize(): Error at offset 49 of 50 bytes in %sbug72663.php on line %d
2525

26-
Notice: unserialize(): Error at offset 79 of 80 bytes in %sbug72663.php on line %d
26+
Notice: unserialize(): Error at offset 82 of 83 bytes in %sbug72663.php on line %d
2727
bool(false)
2828
DONE

ext/standard/tests/strings/bug72663_2.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (!extension_loaded("session")) {
1111

1212
ini_set('session.serialize_handler', 'php_serialize');
1313
session_start();
14-
$sess = 'O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";R:1;}';
14+
$sess = 'O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";s:0:"";}';
1515
session_decode($sess);
1616
var_dump($_SESSION);
1717
?>

sapi/cli/tests/005.phpt

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

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

4242
- Constants [0] {
4343
}
@@ -50,10 +50,10 @@ string(2188) "Class [ <internal:Core> class Exception implements Throwable, Stri
5050

5151
- Properties [7] {
5252
Property [ protected $message = '' ]
53-
Property [ private $string = '' ]
53+
Property [ private string $string = '' ]
5454
Property [ protected $code = 0 ]
55-
Property [ protected $file = NULL ]
56-
Property [ protected $line = NULL ]
55+
Property [ protected string $file = '' ]
56+
Property [ protected int $line = 0 ]
5757
Property [ private array $trace = Array ]
5858
Property [ private ?Throwable $previous = NULL ]
5959
}

tests/lang/038.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Convert warnings to exceptions
55

66
class MyException extends Exception
77
{
8-
function __construct($errstr, $errno=0, $errfile='', $errline='')
8+
function __construct($errstr, $errno=0, $errfile='', $errline=0)
99
{
1010
parent::__construct($errstr, $errno);
1111
$this->file = $errfile;

0 commit comments

Comments
 (0)