From 30a082cb1673cf8406daa025a67b744916a95214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Tue, 20 Apr 2021 22:13:44 +0200 Subject: [PATCH] Make some exception properties typed Closes GH-6891 --- Zend/tests/bug50005.phpt | 2 +- Zend/tests/bug64821.1.phpt | 2 +- Zend/tests/bug64821.2.phpt | 2 +- Zend/tests/bug64821.3.phpt | 4 +-- Zend/zend_exceptions.c | 5 +++- Zend/zend_exceptions.stub.php | 29 ++++++++------------ Zend/zend_exceptions_arginfo.h | 22 +++++++-------- ext/mysqli/mysqli.stub.php | 3 +- ext/mysqli/mysqli_arginfo.h | 4 +-- ext/soap/tests/bug73452.phpt | 2 +- ext/standard/tests/serialize/bug69793.phpt | 2 +- ext/standard/tests/serialize/bug72663_2.phpt | 6 ++-- ext/standard/tests/serialize/bug72663_3.phpt | 2 +- ext/standard/tests/strings/bug72663.phpt | 6 ++-- ext/standard/tests/strings/bug72663_2.phpt | 2 +- sapi/cli/tests/005.phpt | 8 +++--- tests/lang/038.phpt | 2 +- 17 files changed, 49 insertions(+), 54 deletions(-) diff --git a/Zend/tests/bug50005.phpt b/Zend/tests/bug50005.phpt index 8b3817cd5f2..a82a84f7054 100644 --- a/Zend/tests/bug50005.phpt +++ b/Zend/tests/bug50005.phpt @@ -5,7 +5,7 @@ Bug #50005 (Throwing through Reflection modified Exception object makes segmenta class a extends exception { public function __construct() { - $this->file = null; + $this->file = ""; } } diff --git a/Zend/tests/bug64821.1.phpt b/Zend/tests/bug64821.1.phpt index 52713e2211c..2a1a92f715f 100644 --- a/Zend/tests/bug64821.1.phpt +++ b/Zend/tests/bug64821.1.phpt @@ -8,7 +8,7 @@ class a extends exception { $this->message = NULL; $this->string = NULL; $this->code = array(); - $this->line = "hello"; + $this->line = 0; } } diff --git a/Zend/tests/bug64821.2.phpt b/Zend/tests/bug64821.2.phpt index cac0a948f9b..61b17ff6da7 100644 --- a/Zend/tests/bug64821.2.phpt +++ b/Zend/tests/bug64821.2.phpt @@ -5,7 +5,7 @@ Bug #64821 Custom Exceptions crash when internal properties overridden (variatio class a extends exception { public function __construct() { - $this->line = array(); + $this->line = 0; } } diff --git a/Zend/tests/bug64821.3.phpt b/Zend/tests/bug64821.3.phpt index d94f1ba361e..d1f1ec9a516 100644 --- a/Zend/tests/bug64821.3.phpt +++ b/Zend/tests/bug64821.3.phpt @@ -5,8 +5,8 @@ Bug #64821 Custom Exceptions crash when internal properties overridden (variatio class a extends exception { public function __construct() { - $this->line = array(); - $this->file = NULL; + $this->line = 0; + $this->file = ""; } } diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c index 61a0aa923dd..add3bb50a4c 100644 --- a/Zend/zend_exceptions.c +++ b/Zend/zend_exceptions.c @@ -757,8 +757,11 @@ void zend_register_default_exception(void) /* {{{ */ zend_ce_error_exception = register_class_ErrorException(zend_ce_exception); zend_ce_error_exception->create_object = zend_error_exception_new; + /* Declared manually because it uses constant E_ERROR. */ - zend_declare_property_long(zend_ce_error_exception, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED); + zval severity_default_value; + ZVAL_LONG(&severity_default_value, E_ERROR); + 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)); zend_ce_error = register_class_Error(zend_ce_throwable); zend_ce_error->create_object = zend_default_exception_new; diff --git a/Zend/zend_exceptions.stub.php b/Zend/zend_exceptions.stub.php index ece019bbb9c..be00b6b65db 100644 --- a/Zend/zend_exceptions.stub.php +++ b/Zend/zend_exceptions.stub.php @@ -22,16 +22,13 @@ interface Throwable extends Stringable class Exception implements Throwable { - /** @var string */ + /** @var string Intentionally left untyped for BC reasons */ protected $message = ""; - /** @var string */ - private $string = ""; - /** @var int */ + private string $string = ""; + /** @var int Intentionally left untyped for BC reasons */ protected $code = 0; - /** @var string|null */ - protected $file = null; - /** @var int|null */ - protected $line = null; + protected string $file = ""; + protected int $line = 0; private array $trace = []; private ?Throwable $previous = null; @@ -62,8 +59,7 @@ class Exception implements Throwable class ErrorException extends Exception { - /** @var int */ - protected $severity = E_ERROR; + protected int $severity = E_ERROR; public function __construct( string $message = "", @@ -79,16 +75,13 @@ class ErrorException extends Exception class Error implements Throwable { - /** @var string */ + /** @var string Intentionally left untyped for BC reasons */ protected $message = ""; - /** @var string */ - private $string = ""; - /** @var int */ + private string $string = ""; + /** @var int Intentionally left untyped for BC reasons */ protected $code = 0; - /** @var string|null */ - protected $file = null; - /** @var int|null */ - protected $line = null; + protected string $file = ""; + protected int $line; private array $trace = []; private ?Throwable $previous = null; diff --git a/Zend/zend_exceptions_arginfo.h b/Zend/zend_exceptions_arginfo.h index fd92ff06355..4779d698e19 100644 --- a/Zend/zend_exceptions_arginfo.h +++ b/Zend/zend_exceptions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: ef8c275a543d67fd96a775b0b18098ccdb428285 */ + * Stub hash: 9a9ce2975a7449a621d364beca646525fc56b294 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Throwable_getMessage, 0, 0, IS_STRING, 0) ZEND_END_ARG_INFO() @@ -214,7 +214,7 @@ static zend_class_entry *register_class_Exception(zend_class_entry *class_entry_ zval property_string_default_value; ZVAL_EMPTY_STRING(&property_string_default_value); zend_string *property_string_name = zend_string_init("string", sizeof("string") - 1, 1); - zend_declare_property_ex(class_entry, property_string_name, &property_string_default_value, ZEND_ACC_PRIVATE, NULL); + 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)); zend_string_release(property_string_name); zval property_code_default_value; @@ -224,15 +224,15 @@ static zend_class_entry *register_class_Exception(zend_class_entry *class_entry_ zend_string_release(property_code_name); zval property_file_default_value; - ZVAL_NULL(&property_file_default_value); + ZVAL_EMPTY_STRING(&property_file_default_value); zend_string *property_file_name = zend_string_init("file", sizeof("file") - 1, 1); - zend_declare_property_ex(class_entry, property_file_name, &property_file_default_value, ZEND_ACC_PROTECTED, NULL); + 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)); zend_string_release(property_file_name); zval property_line_default_value; - ZVAL_NULL(&property_line_default_value); + ZVAL_LONG(&property_line_default_value, 0); zend_string *property_line_name = zend_string_init("line", sizeof("line") - 1, 1); - zend_declare_property_ex(class_entry, property_line_name, &property_line_default_value, ZEND_ACC_PROTECTED, NULL); + 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)); zend_string_release(property_line_name); zval property_trace_default_value; @@ -278,7 +278,7 @@ static zend_class_entry *register_class_Error(zend_class_entry *class_entry_Thro zval property_string_default_value; ZVAL_EMPTY_STRING(&property_string_default_value); zend_string *property_string_name = zend_string_init("string", sizeof("string") - 1, 1); - zend_declare_property_ex(class_entry, property_string_name, &property_string_default_value, ZEND_ACC_PRIVATE, NULL); + 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)); zend_string_release(property_string_name); zval property_code_default_value; @@ -288,15 +288,15 @@ static zend_class_entry *register_class_Error(zend_class_entry *class_entry_Thro zend_string_release(property_code_name); zval property_file_default_value; - ZVAL_NULL(&property_file_default_value); + ZVAL_EMPTY_STRING(&property_file_default_value); zend_string *property_file_name = zend_string_init("file", sizeof("file") - 1, 1); - zend_declare_property_ex(class_entry, property_file_name, &property_file_default_value, ZEND_ACC_PROTECTED, NULL); + 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)); zend_string_release(property_file_name); zval property_line_default_value; - ZVAL_NULL(&property_line_default_value); + ZVAL_UNDEF(&property_line_default_value); zend_string *property_line_name = zend_string_init("line", sizeof("line") - 1, 1); - zend_declare_property_ex(class_entry, property_line_name, &property_line_default_value, ZEND_ACC_PROTECTED, NULL); + 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)); zend_string_release(property_line_name); zval property_trace_default_value; diff --git a/ext/mysqli/mysqli.stub.php b/ext/mysqli/mysqli.stub.php index 56673b3b54b..99ce77dc4e6 100644 --- a/ext/mysqli/mysqli.stub.php +++ b/ext/mysqli/mysqli.stub.php @@ -599,8 +599,7 @@ final class mysqli_warning final class mysqli_sql_exception extends RuntimeException { - /** @var string */ - protected $sqlstate = "00000"; + protected string $sqlstate = "00000"; } function mysqli_affected_rows(mysqli $mysql): int|string {} diff --git a/ext/mysqli/mysqli_arginfo.h b/ext/mysqli/mysqli_arginfo.h index 23e14cd39b7..fe450482527 100644 --- a/ext/mysqli/mysqli_arginfo.h +++ b/ext/mysqli/mysqli_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 14c5c4960e70f5b38fc6c4f4403804cc28147b0b */ + * Stub hash: b0232d18f570208d673ad7535ca60997e038acb8 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mysqli_affected_rows, 0, 1, MAY_BE_LONG|MAY_BE_STRING) 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 zend_string *property_sqlstate_default_value_str = zend_string_init("00000", sizeof("00000") - 1, 1); ZVAL_STR(&property_sqlstate_default_value, property_sqlstate_default_value_str); zend_string *property_sqlstate_name = zend_string_init("sqlstate", sizeof("sqlstate") - 1, 1); - zend_declare_property_ex(class_entry, property_sqlstate_name, &property_sqlstate_default_value, ZEND_ACC_PROTECTED, NULL); + 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)); zend_string_release(property_sqlstate_name); return class_entry; diff --git a/ext/soap/tests/bug73452.phpt b/ext/soap/tests/bug73452.phpt index 57a5786a3f3..22b8aafdf36 100644 --- a/ext/soap/tests/bug73452.phpt +++ b/ext/soap/tests/bug73452.phpt @@ -5,7 +5,7 @@ soap --FILE-- diff --git a/ext/standard/tests/serialize/bug69793.phpt b/ext/standard/tests/serialize/bug69793.phpt index 6c2d1df0262..1ebc831a9ad 100644 --- a/ext/standard/tests/serialize/bug69793.phpt +++ b/ext/standard/tests/serialize/bug69793.phpt @@ -2,7 +2,7 @@ Bug #69793: Remotely triggerable stack exhaustion via recursive method calls --FILE-- diff --git a/ext/standard/tests/serialize/bug72663_2.phpt b/ext/standard/tests/serialize/bug72663_2.phpt index 8825dc5efc3..2d7fa772ec4 100644 --- a/ext/standard/tests/serialize/bug72663_2.phpt +++ b/ext/standard/tests/serialize/bug72663_2.phpt @@ -13,7 +13,7 @@ class obj implements Serializable { } } -$inner = 'a:1:{i:0;O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";R:4;}'; +$inner = 'a:1:{i:0;O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";s:0:"";}'; $exploit = 'a:2:{i:0;C:3:"obj":'.strlen($inner).':{'.$inner.'}i:1;R:4;}'; var_dump(unserialize($exploit)); @@ -21,7 +21,7 @@ var_dump(unserialize($exploit)); --EXPECTF-- Notice: unserialize(): Unexpected end of serialized data in %s on line %d -Notice: unserialize(): Error at offset 46 of 47 bytes in %s on line %d +Notice: unserialize(): Error at offset 49 of 50 bytes in %s on line %d -Notice: unserialize(): Error at offset 79 of 80 bytes in %s on line %d +Notice: unserialize(): Error at offset 82 of 83 bytes in %s on line %d bool(false) diff --git a/ext/standard/tests/serialize/bug72663_3.phpt b/ext/standard/tests/serialize/bug72663_3.phpt index 706bdf21fb9..c3051a685d8 100644 --- a/ext/standard/tests/serialize/bug72663_3.phpt +++ b/ext/standard/tests/serialize/bug72663_3.phpt @@ -7,7 +7,7 @@ session.serialize_handler=php_serialize --FILE-- diff --git a/ext/standard/tests/strings/bug72663.phpt b/ext/standard/tests/strings/bug72663.phpt index c9abd51b080..384ab595bf2 100644 --- a/ext/standard/tests/strings/bug72663.phpt +++ b/ext/standard/tests/strings/bug72663.phpt @@ -12,7 +12,7 @@ class obj implements Serializable { } } -$inner = 'a:1:{i:0;O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";R:4;}'; +$inner = 'a:1:{i:0;O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";s:0:"";}'; $exploit = 'a:2:{i:0;C:3:"obj":'.strlen($inner).':{'.$inner.'}i:1;R:4;}'; var_dump(unserialize($exploit)); @@ -21,8 +21,8 @@ DONE --EXPECTF-- Notice: unserialize(): Unexpected end of serialized data in %sbug72663.php on line %d -Notice: unserialize(): Error at offset 46 of 47 bytes in %sbug72663.php on line %d +Notice: unserialize(): Error at offset 49 of 50 bytes in %sbug72663.php on line %d -Notice: unserialize(): Error at offset 79 of 80 bytes in %sbug72663.php on line %d +Notice: unserialize(): Error at offset 82 of 83 bytes in %sbug72663.php on line %d bool(false) DONE diff --git a/ext/standard/tests/strings/bug72663_2.phpt b/ext/standard/tests/strings/bug72663_2.phpt index d0d7f810085..6d626e01754 100644 --- a/ext/standard/tests/strings/bug72663_2.phpt +++ b/ext/standard/tests/strings/bug72663_2.phpt @@ -11,7 +11,7 @@ if (!extension_loaded("session")) { ini_set('session.serialize_handler', 'php_serialize'); session_start(); -$sess = 'O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";R:1;}'; +$sess = 'O:9:"Exception":2:{s:7:"'."\0".'*'."\0".'file";s:0:"";}'; session_decode($sess); var_dump($_SESSION); ?> diff --git a/sapi/cli/tests/005.phpt b/sapi/cli/tests/005.phpt index de2ca28a1fe..be4fff528cc 100644 --- a/sapi/cli/tests/005.phpt +++ b/sapi/cli/tests/005.phpt @@ -37,7 +37,7 @@ string(183) "Class [ class stdClass ] { } " -string(2188) "Class [ class Exception implements Throwable, Stringable ] { +string(2201) "Class [ class Exception implements Throwable, Stringable ] { - Constants [0] { } @@ -50,10 +50,10 @@ string(2188) "Class [ class Exception implements Throwable, Stri - Properties [7] { Property [ protected $message = '' ] - Property [ private $string = '' ] + Property [ private string $string = '' ] Property [ protected $code = 0 ] - Property [ protected $file = NULL ] - Property [ protected $line = NULL ] + Property [ protected string $file = '' ] + Property [ protected int $line = 0 ] Property [ private array $trace = Array ] Property [ private ?Throwable $previous = NULL ] } diff --git a/tests/lang/038.phpt b/tests/lang/038.phpt index 677c11be9c8..915969f3662 100644 --- a/tests/lang/038.phpt +++ b/tests/lang/038.phpt @@ -5,7 +5,7 @@ Convert warnings to exceptions class MyException extends Exception { - function __construct($errstr, $errno=0, $errfile='', $errline='') + function __construct($errstr, $errno=0, $errfile='', $errline=0) { parent::__construct($errstr, $errno); $this->file = $errfile;