Skip to content

Improve default value handling of Exception constructors #6166

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 2 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
63 changes: 63 additions & 0 deletions Zend/tests/ErrorException_construct.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
--TEST--
Test default value handling of ErrorException::__construct()
--FILE--
<?php

$e = new ErrorException();
var_dump($e->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)
15 changes: 9 additions & 6 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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);
}
}
/* }}} */
Expand Down
6 changes: 3 additions & 3 deletions Zend/zend_exceptions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand All @@ -48,7 +48,7 @@ public function __toString(): string {}

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 {}
}
Expand All @@ -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() {}
Expand Down
10 changes: 5 additions & 5 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: 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()
Expand All @@ -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()
Expand All @@ -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()

Expand Down
4 changes: 2 additions & 2 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(2177) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {
string(2170) "Class [ <internal:Core> class Exception implements Throwable, Stringable ] {

- Constants [0] {
}
Expand Down Expand Up @@ -68,7 +68,7 @@ string(2177) "Class [ <internal:Core> class Exception implements Throwable, Stri
Method [ <internal:Core, ctor> public method __construct ] {

- Parameters [3] {
Parameter #0 [ <optional> string $message = <default> ]
Parameter #0 [ <optional> string $message = "" ]
Parameter #1 [ <optional> int $code = 0 ]
Parameter #2 [ <optional> ?Throwable $previous = null ]
}
Expand Down