Skip to content

Commit 4c821cf

Browse files
committed
Improve default value handling of Exception constructors
Closes GH-6166
1 parent 9f074a5 commit 4c821cf

File tree

5 files changed

+82
-16
lines changed

5 files changed

+82
-16
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
--TEST--
2+
Test default value handling of ErrorException::__construct()
3+
--FILE--
4+
<?php
5+
6+
$e = new ErrorException();
7+
var_dump($e->getMessage());
8+
var_dump($e->getFile());
9+
var_dump($e->getLine());
10+
11+
$e = new ErrorException("Second", 0, E_ERROR, null);
12+
var_dump($e->getMessage());
13+
var_dump($e->getFile());
14+
var_dump($e->getLine());
15+
16+
$e = new ErrorException("Third", 0, E_ERROR, null, null);
17+
var_dump($e->getMessage());
18+
var_dump($e->getFile());
19+
var_dump($e->getLine());
20+
21+
$e = new ErrorException("Forth", 0, E_ERROR, null, 123);
22+
var_dump($e->getMessage());
23+
var_dump($e->getFile());
24+
var_dump($e->getLine());
25+
26+
$e = new ErrorException("Fifth", 0, E_ERROR, "abc.php");
27+
var_dump($e->getMessage());
28+
var_dump($e->getFile());
29+
var_dump($e->getLine());
30+
31+
$e = new ErrorException("Sixth", 0, E_ERROR, "abc.php", null);
32+
var_dump($e->getMessage());
33+
var_dump($e->getFile());
34+
var_dump($e->getLine());
35+
36+
$e = new ErrorException("Seventh", 0, E_ERROR, "abc.php", 123);
37+
var_dump($e->getMessage());
38+
var_dump($e->getFile());
39+
var_dump($e->getLine());
40+
41+
?>
42+
--EXPECTF--
43+
string(0) ""
44+
string(%d) "%sErrorException_construct.php"
45+
int(3)
46+
string(6) "Second"
47+
string(%d) "%sErrorException_construct.php"
48+
int(8)
49+
string(5) "Third"
50+
string(%d) "%sErrorException_construct.php"
51+
int(13)
52+
string(5) "Forth"
53+
string(%d) "%sErrorException_construct.php"
54+
int(123)
55+
string(5) "Fifth"
56+
string(7) "abc.php"
57+
int(0)
58+
string(5) "Sixth"
59+
string(7) "abc.php"
60+
int(0)
61+
string(7) "Seventh"
62+
string(7) "abc.php"
63+
int(123)

Zend/zend_exceptions.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,10 @@ ZEND_METHOD(ErrorException, __construct)
334334
{
335335
zend_string *message = NULL, *filename = NULL;
336336
zend_long code = 0, severity = E_ERROR, lineno;
337+
zend_bool lineno_is_null = 1;
337338
zval tmp, *object, *previous = NULL;
338-
int argc = ZEND_NUM_ARGS();
339339

340-
if (zend_parse_parameters(argc, "|SllSlO!", &message, &code, &severity, &filename, &lineno, &previous, zend_ce_throwable) == FAILURE) {
340+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SllS!l!O!", &message, &code, &severity, &filename, &lineno, &lineno_is_null, &previous, zend_ce_throwable) == FAILURE) {
341341
RETURN_THROWS();
342342
}
343343

@@ -361,15 +361,18 @@ ZEND_METHOD(ErrorException, __construct)
361361
ZVAL_LONG(&tmp, severity);
362362
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
363363

364-
if (argc >= 4) {
364+
if (filename) {
365365
ZVAL_STR_COPY(&tmp, filename);
366366
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
367367
zval_ptr_dtor(&tmp);
368-
if (argc < 5) {
369-
lineno = 0; /* invalidate lineno */
370-
}
368+
}
369+
370+
if (!lineno_is_null) {
371371
ZVAL_LONG(&tmp, lineno);
372372
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
373+
} else if (filename) {
374+
ZVAL_LONG(&tmp, 0);
375+
zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
373376
}
374377
}
375378
/* }}} */

Zend/zend_exceptions.stub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Exception implements Throwable
2424
{
2525
final private function __clone() {}
2626

27-
public function __construct(string $message = UNKNOWN, int $code = 0, ?Throwable $previous = null) {}
27+
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) {}
2828

2929
public function __wakeup() {}
3030

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

4949
class ErrorException extends Exception
5050
{
51-
public function __construct(string $message = UNKNOWN, int $code = 0, int $severity = E_ERROR, string $filename = UNKNOWN, int $lineno = 0, ?Throwable $previous = null) {}
51+
public function __construct(string $message = "", int $code = 0, int $severity = E_ERROR, ?string $filename = null, ?int $line = null, ?Throwable $previous = null) {}
5252

5353
final public function getSeverity(): int {}
5454
}
@@ -59,7 +59,7 @@ class Error implements Throwable
5959
final private function __clone() {}
6060

6161
/** @implementation-alias Exception::__construct */
62-
public function __construct(string $message = UNKNOWN, int $code = 0, ?Throwable $previous = null) {}
62+
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null) {}
6363

6464
/** @implementation-alias Exception::__wakeup */
6565
public function __wakeup() {}

Zend/zend_exceptions_arginfo.h

Lines changed: 5 additions & 5 deletions
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: d0679a3c11f089dcb31cfdfe56f0336ceba301a9 */
2+
* Stub hash: bc49b326136997660887b12f0c59f8a57b17ecaf */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Throwable_getMessage, 0, 0, IS_STRING, 0)
55
ZEND_END_ARG_INFO()
@@ -23,7 +23,7 @@ ZEND_END_ARG_INFO()
2323
#define arginfo_class_Exception___clone arginfo_class_Throwable_getCode
2424

2525
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Exception___construct, 0, 0, 0)
26-
ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0)
26+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 0, "\"\"")
2727
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, code, IS_LONG, 0, "0")
2828
ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, previous, Throwable, 1, "null")
2929
ZEND_END_ARG_INFO()
@@ -47,11 +47,11 @@ ZEND_END_ARG_INFO()
4747
#define arginfo_class_Exception___toString arginfo_class_Throwable_getMessage
4848

4949
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ErrorException___construct, 0, 0, 0)
50-
ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0)
50+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 0, "\"\"")
5151
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, code, IS_LONG, 0, "0")
5252
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, severity, IS_LONG, 0, "E_ERROR")
53-
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
54-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, lineno, IS_LONG, 0, "0")
53+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, filename, IS_STRING, 1, "null")
54+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, line, IS_LONG, 1, "null")
5555
ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(0, previous, Throwable, 1, "null")
5656
ZEND_END_ARG_INFO()
5757

sapi/cli/tests/005.phpt

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

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

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

7070
- Parameters [3] {
71-
Parameter #0 [ <optional> string $message = <default> ]
71+
Parameter #0 [ <optional> string $message = "" ]
7272
Parameter #1 [ <optional> int $code = 0 ]
7373
Parameter #2 [ <optional> ?Throwable $previous = null ]
7474
}

0 commit comments

Comments
 (0)