Skip to content

Commit d6505ac

Browse files
committed
Fixed bug #64821 Custom Exceptions crash when internal properties overridden
If user inherits Exception and overrides the properties to arbitrary data types, or simply doesn't run parent::__construct(), here we go. Just convert everything to the appropriate data type, like Exception::__toString() does.
1 parent 1cc2162 commit d6505ac

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ PHP NEWS
1515
. Fixed bug #64770 (stream_select() fails with pipes returned by proc_open()
1616
on Windows x64). (Anatol)
1717

18+
- Zend Engine:
19+
. Fixed bug #64821 (Custom Exception crash when internal properties overridden).
20+
(Anatol)
21+
1822
09 May 2013, PHP 5.3.25
1923

2024
- Core:

Zend/tests/bug64821.1.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Bug #64821 Custom Exceptions crash when internal properties overridden (variation 1)
3+
--FILE--
4+
<?php
5+
6+
class a extends exception {
7+
public function __construct() {
8+
$this->message = NULL;
9+
$this->string = NULL;
10+
$this->code = array();
11+
$this->line = "hello";
12+
}
13+
}
14+
15+
throw new a;
16+
17+
?>
18+
--EXPECTF--
19+
Fatal error: Uncaught exception 'a' in %s:0
20+
Stack trace:
21+
#0 {main}
22+
thrown in %s on line %d

Zend/tests/bug64821.2.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug #64821 Custom Exceptions crash when internal properties overridden (variation 2)
3+
--FILE--
4+
<?php
5+
6+
class a extends exception {
7+
public function __construct() {
8+
$this->line = array();
9+
}
10+
}
11+
12+
throw new a;
13+
14+
?>
15+
--EXPECTF--
16+
Fatal error: Uncaught exception 'a' in %s:0
17+
Stack trace:
18+
#0 {main}
19+
thrown in %s on line %d

Zend/tests/bug64821.3.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug #64821 Custom Exceptions crash when internal properties overridden (variation 3)
3+
--FILE--
4+
<?php
5+
6+
class a extends exception {
7+
public function __construct() {
8+
$this->line = array();
9+
$this->file = NULL;
10+
}
11+
}
12+
13+
throw new a;
14+
15+
?>
16+
--EXPECTF--
17+
Fatal error: Uncaught exception 'a' in :0
18+
Stack trace:
19+
#0 {main}
20+
thrown in Unknown on line %d

Zend/zend_exceptions.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,10 @@ ZEND_API void zend_exception_error(zval *exception, int severity TSRMLS_DC) /* {
803803
if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
804804
file = zend_read_property(default_exception_ce, EG(exception), "file", sizeof("file")-1, 1 TSRMLS_CC);
805805
line = zend_read_property(default_exception_ce, EG(exception), "line", sizeof("line")-1, 1 TSRMLS_CC);
806+
807+
convert_to_string(file);
808+
file = (Z_STRLEN_P(file) > 0) ? file : NULL;
809+
line = (Z_TYPE_P(line) == IS_LONG) ? line : NULL;
806810
} else {
807811
file = NULL;
808812
line = NULL;
@@ -814,7 +818,11 @@ ZEND_API void zend_exception_error(zval *exception, int severity TSRMLS_DC) /* {
814818
file = zend_read_property(default_exception_ce, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
815819
line = zend_read_property(default_exception_ce, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
816820

817-
zend_error_va(severity, Z_STRVAL_P(file), Z_LVAL_P(line), "Uncaught %s\n thrown", Z_STRVAL_P(str));
821+
convert_to_string(str);
822+
convert_to_string(file);
823+
convert_to_long(line);
824+
825+
zend_error_va(severity, (Z_STRLEN_P(file) > 0) ? Z_STRVAL_P(file) : NULL, Z_LVAL_P(line), "Uncaught %s\n thrown", Z_STRVAL_P(str));
818826
} else {
819827
zend_error(severity, "Uncaught exception '%s'", ce_exception->name);
820828
}

0 commit comments

Comments
 (0)