Skip to content

Make some exception properties typed #6891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Zend/tests/bug50005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
}
}

Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/bug64821.1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class a extends exception {
$this->message = NULL;
$this->string = NULL;
$this->code = array();
$this->line = "hello";
$this->line = 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/bug64821.2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Zend/tests/bug64821.3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
}
}

Expand Down
5 changes: 4 additions & 1 deletion Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 11 additions & 18 deletions Zend/zend_exceptions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ public function getTraceAsString(): string;

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;

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

class ErrorException extends Exception
{
/** @var int */
protected $severity = E_ERROR;
protected int $severity = E_ERROR;

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

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;

Expand Down
22 changes: 11 additions & 11 deletions Zend/zend_exceptions_arginfo.h
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions ext/mysqli/mysqli.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,7 @@ public function next(): bool {}

final class mysqli_sql_exception extends RuntimeException
{
/** @var string */
protected $sqlstate = "00000";
protected string $sqlstate = "00000";
}

function mysqli_affected_rows(mysqli $mysql): int|string {}
Expand Down
4 changes: 2 additions & 2 deletions ext/mysqli/mysqli_arginfo.h
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ext/soap/tests/bug73452.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ soap
--FILE--
<?php

$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";}';
$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;}';
echo unserialize($data);

?>
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/tests/serialize/bug69793.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Bug #69793: Remotely triggerable stack exhaustion via recursive method calls
--FILE--
<?php
$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;}');
$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;}');

var_dump($e."");
?>
Expand Down
6 changes: 3 additions & 3 deletions ext/standard/tests/serialize/bug72663_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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));

?>
--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)
2 changes: 1 addition & 1 deletion ext/standard/tests/serialize/bug72663_3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ session.serialize_handler=php_serialize
--FILE--
<?php
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);
?>
Expand Down
6 changes: 3 additions & 3 deletions ext/standard/tests/strings/bug72663.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
2 changes: 1 addition & 1 deletion ext/standard/tests/strings/bug72663_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
?>
Expand Down
8 changes: 4 additions & 4 deletions sapi/cli/tests/005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ string(183) "Class [ <internal:Core> class stdClass ] {
}

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

- Constants [0] {
}
Expand All @@ -50,10 +50,10 @@ string(2188) "Class [ <internal:Core> 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 ]
}
Expand Down
2 changes: 1 addition & 1 deletion tests/lang/038.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down