From 4c821cf20668776737272587e7a87876b4fca25e Mon Sep 17 00:00:00 2001 From: =?utf8?q?M=C3=A1t=C3=A9=20Kocsis?= Date: Sat, 19 Sep 2020 14:13:42 +0200 Subject: [PATCH] Improve default value handling of Exception constructors Closes GH-6166 --- Zend/tests/ErrorException_construct.phpt | 63 ++++++++++++++++++++++++ Zend/zend_exceptions.c | 15 +++--- Zend/zend_exceptions.stub.php | 6 +-- Zend/zend_exceptions_arginfo.h | 10 ++-- sapi/cli/tests/005.phpt | 4 +- 5 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 Zend/tests/ErrorException_construct.phpt diff --git a/Zend/tests/ErrorException_construct.phpt b/Zend/tests/ErrorException_construct.phpt new file mode 100644 index 0000000000..7304af3da0 --- /dev/null +++ b/Zend/tests/ErrorException_construct.phpt @@ -0,0 +1,63 @@ +--TEST-- +Test default value handling of ErrorException::__construct() +--FILE-- +getMessage()); +var_dump($e->getFile()); +var_dump($e->getLine()); + +$e = new ErrorException("Second", 0, E_ERROR, null); +var_dump($e->getMessage()); +var_dump($e->getFile()); +var_dump($e->getLine()); + +$e = new ErrorException("Third", 0, E_ERROR, null, null); +var_dump($e->getMessage()); +var_dump($e->getFile()); +var_dump($e->getLine()); + +$e = new ErrorException("Forth", 0, E_ERROR, null, 123); +var_dump($e->getMessage()); +var_dump($e->getFile()); +var_dump($e->getLine()); + +$e = new ErrorException("Fifth", 0, E_ERROR, "abc.php"); +var_dump($e->getMessage()); +var_dump($e->getFile()); +var_dump($e->getLine()); + +$e = new ErrorException("Sixth", 0, E_ERROR, "abc.php", null); +var_dump($e->getMessage()); +var_dump($e->getFile()); +var_dump($e->getLine()); + +$e = new ErrorException("Seventh", 0, E_ERROR, "abc.php", 123); +var_dump($e->getMessage()); +var_dump($e->getFile()); +var_dump($e->getLine()); + +?> +--EXPECTF-- +string(0) "" +string(%d) "%sErrorException_construct.php" +int(3) +string(6) "Second" +string(%d) "%sErrorException_construct.php" +int(8) +string(5) "Third" +string(%d) "%sErrorException_construct.php" +int(13) +string(5) "Forth" +string(%d) "%sErrorException_construct.php" +int(123) +string(5) "Fifth" +string(7) "abc.php" +int(0) +string(5) "Sixth" +string(7) "abc.php" +int(0) +string(7) "Seventh" +string(7) "abc.php" +int(123) diff --git a/Zend/zend_exceptions.c b/Zend/zend_exceptions.c index 1a0b7d581a..1f47d88997 100644 --- a/Zend/zend_exceptions.c +++ b/Zend/zend_exceptions.c @@ -334,10 +334,10 @@ ZEND_METHOD(ErrorException, __construct) { zend_string *message = NULL, *filename = NULL; zend_long code = 0, severity = E_ERROR, lineno; + zend_bool lineno_is_null = 1; zval tmp, *object, *previous = NULL; - int argc = ZEND_NUM_ARGS(); - if (zend_parse_parameters(argc, "|SllSlO!", &message, &code, &severity, &filename, &lineno, &previous, zend_ce_throwable) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SllS!l!O!", &message, &code, &severity, &filename, &lineno, &lineno_is_null, &previous, zend_ce_throwable) == FAILURE) { RETURN_THROWS(); } @@ -361,15 +361,18 @@ ZEND_METHOD(ErrorException, __construct) ZVAL_LONG(&tmp, severity); zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp); - if (argc >= 4) { + if (filename) { ZVAL_STR_COPY(&tmp, filename); zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_FILE), &tmp); zval_ptr_dtor(&tmp); - if (argc < 5) { - lineno = 0; /* invalidate lineno */ - } + } + + if (!lineno_is_null) { ZVAL_LONG(&tmp, lineno); zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp); + } else if (filename) { + ZVAL_LONG(&tmp, 0); + zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp); } } /* }}} */ diff --git a/Zend/zend_exceptions.stub.php b/Zend/zend_exceptions.stub.php index bc90b6f1b5..f22fb3eae5 100644 --- a/Zend/zend_exceptions.stub.php +++ b/Zend/zend_exceptions.stub.php @@ -24,7 +24,7 @@ class Exception implements Throwable { final private function __clone() {} - public function __construct(string $message = UNKNOWN, int $code = 0, ?Throwable $previous = null) {} + public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) {} public function __wakeup() {} @@ -48,7 +48,7 @@ class Exception implements Throwable class ErrorException extends Exception { - public function __construct(string $message = UNKNOWN, int $code = 0, int $severity = E_ERROR, string $filename = UNKNOWN, int $lineno = 0, ?Throwable $previous = null) {} + public function __construct(string $message = "", int $code = 0, int $severity = E_ERROR, ?string $filename = null, ?int $line = null, ?Throwable $previous = null) {} final public function getSeverity(): int {} } @@ -59,7 +59,7 @@ class Error implements Throwable final private function __clone() {} /** @implementation-alias Exception::__construct */ - public function __construct(string $message = UNKNOWN, int $code = 0, ?Throwable $previous = null) {} + public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) {} /** @implementation-alias Exception::__wakeup */ public function __wakeup() {} diff --git a/Zend/zend_exceptions_arginfo.h b/Zend/zend_exceptions_arginfo.h index fbab0aa90e..e8ea0a59a8 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: d0679a3c11f089dcb31cfdfe56f0336ceba301a9 */ + * Stub hash: bc49b326136997660887b12f0c59f8a57b17ecaf */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Throwable_getMessage, 0, 0, IS_STRING, 0) ZEND_END_ARG_INFO() @@ -23,7 +23,7 @@ ZEND_END_ARG_INFO() #define arginfo_class_Exception___clone arginfo_class_Throwable_getCode ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Exception___construct, 0, 0, 0) - ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 0, "\"\"") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, code, IS_LONG, 0, "0") ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, previous, Throwable, 1, "null") ZEND_END_ARG_INFO() @@ -47,11 +47,11 @@ ZEND_END_ARG_INFO() #define arginfo_class_Exception___toString arginfo_class_Throwable_getMessage ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ErrorException___construct, 0, 0, 0) - ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 0, "\"\"") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, code, IS_LONG, 0, "0") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, severity, IS_LONG, 0, "E_ERROR") - ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0) - ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, lineno, IS_LONG, 0, "0") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filename, IS_STRING, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, line, IS_LONG, 1, "null") ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, previous, Throwable, 1, "null") ZEND_END_ARG_INFO() diff --git a/sapi/cli/tests/005.phpt b/sapi/cli/tests/005.phpt index a1d51088ab..ca3dfd9730 100644 --- a/sapi/cli/tests/005.phpt +++ b/sapi/cli/tests/005.phpt @@ -37,7 +37,7 @@ string(183) "Class [ class stdClass ] { } " -string(2177) "Class [ class Exception implements Throwable, Stringable ] { +string(2170) "Class [ class Exception implements Throwable, Stringable ] { - Constants [0] { } @@ -68,7 +68,7 @@ string(2177) "Class [ class Exception implements Throwable, Stri Method [ public method __construct ] { - Parameters [3] { - Parameter #0 [ string $message = ] + Parameter #0 [ string $message = "" ] Parameter #1 [ int $code = 0 ] Parameter #2 [ ?Throwable $previous = null ] } -- 2.40.0