Skip to content

Commit af37d58

Browse files
committed
Fix assertion in Exception::getMessage() if $message is a ref
And same for other properties. Encountered in Symfony.
1 parent 1c22ace commit af37d58

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Calling exception getters when properties hold references
3+
--FILE--
4+
<?php
5+
6+
class MyException extends Exception {
7+
public function __construct(&$refMsg, &$refCode, &$refFile, &$refLine) {
8+
$this->message =& $refMsg;
9+
$this->code =& $refCode;
10+
$this->file =& $refFile;
11+
$this->line =& $refLine;
12+
}
13+
}
14+
15+
$refMsg = "foo";
16+
$refCode = 0;
17+
$refFile = "foobar";
18+
$refLine = 42;
19+
$ex = new MyException($refMsg, $refCode, $refFile, $refLine);
20+
var_dump($ex->getMessage());
21+
var_dump($ex->getCode());
22+
var_dump($ex->getFile());
23+
var_dump($ex->getLine());
24+
25+
?>
26+
--EXPECT--
27+
string(3) "foo"
28+
int(0)
29+
string(6) "foobar"
30+
int(42)

Zend/zend_exceptions.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,71 +401,83 @@ ZEND_METHOD(error_exception, __construct)
401401
Get the file in which the exception occurred */
402402
ZEND_METHOD(exception, getFile)
403403
{
404-
zval rv;
404+
zval *prop, rv;
405405

406406
DEFAULT_0_PARAMS;
407407

408-
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), ZEND_STR_FILE));
408+
prop = GET_PROPERTY(getThis(), ZEND_STR_FILE);
409+
ZVAL_DEREF(prop);
410+
ZVAL_COPY(return_value, prop);
409411
}
410412
/* }}} */
411413

412414
/* {{{ proto int Exception|Error::getLine()
413415
Get the line in which the exception occurred */
414416
ZEND_METHOD(exception, getLine)
415417
{
416-
zval rv;
418+
zval *prop, rv;
417419

418420
DEFAULT_0_PARAMS;
419421

420-
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), ZEND_STR_LINE));
422+
prop = GET_PROPERTY(getThis(), ZEND_STR_LINE);
423+
ZVAL_DEREF(prop);
424+
ZVAL_COPY(return_value, prop);
421425
}
422426
/* }}} */
423427

424428
/* {{{ proto string Exception|Error::getMessage()
425429
Get the exception message */
426430
ZEND_METHOD(exception, getMessage)
427431
{
428-
zval rv;
432+
zval *prop, rv;
429433

430434
DEFAULT_0_PARAMS;
431435

432-
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), ZEND_STR_MESSAGE));
436+
prop = GET_PROPERTY(getThis(), ZEND_STR_MESSAGE);
437+
ZVAL_DEREF(prop);
438+
ZVAL_COPY(return_value, prop);
433439
}
434440
/* }}} */
435441

436442
/* {{{ proto int Exception|Error::getCode()
437443
Get the exception code */
438444
ZEND_METHOD(exception, getCode)
439445
{
440-
zval rv;
446+
zval *prop, rv;
441447

442448
DEFAULT_0_PARAMS;
443449

444-
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), ZEND_STR_CODE));
450+
prop = GET_PROPERTY(getThis(), ZEND_STR_CODE);
451+
ZVAL_DEREF(prop);
452+
ZVAL_COPY(return_value, prop);
445453
}
446454
/* }}} */
447455

448456
/* {{{ proto array Exception|Error::getTrace()
449457
Get the stack trace for the location in which the exception occurred */
450458
ZEND_METHOD(exception, getTrace)
451459
{
452-
zval rv;
460+
zval *prop, rv;
453461

454462
DEFAULT_0_PARAMS;
455463

456-
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), ZEND_STR_TRACE));
464+
prop = GET_PROPERTY(getThis(), ZEND_STR_TRACE);
465+
ZVAL_DEREF(prop);
466+
ZVAL_COPY(return_value, prop);
457467
}
458468
/* }}} */
459469

460470
/* {{{ proto int ErrorException::getSeverity()
461471
Get the exception severity */
462472
ZEND_METHOD(error_exception, getSeverity)
463473
{
464-
zval rv;
474+
zval *prop, rv;
465475

466476
DEFAULT_0_PARAMS;
467477

468-
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), ZEND_STR_SEVERITY));
478+
prop = GET_PROPERTY(getThis(), ZEND_STR_SEVERITY);
479+
ZVAL_DEREF(prop);
480+
ZVAL_COPY(return_value, prop);
469481
}
470482
/* }}} */
471483

0 commit comments

Comments
 (0)