Skip to content

Commit 2098cc7

Browse files
committed
Merge branch 'PHP-5.4' into PHP-5.5
2 parents bef54e5 + ef37055 commit 2098cc7

File tree

2 files changed

+92
-15
lines changed

2 files changed

+92
-15
lines changed

Zend/tests/bug63762.phpt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
Bug #63762 - Sigsegv when Exception::$trace is changed by user
3+
--FILE--
4+
<?php
5+
$e = new Exception();
6+
7+
$ref = new ReflectionProperty($e, 'trace');
8+
$ref->setAccessible(TRUE);
9+
10+
echo "Array of NULL:\n";
11+
$ref->setValue($e, array(NULL));
12+
13+
var_dump($e->getTraceAsString());
14+
15+
echo "\nArray of empty array:\n";
16+
$ref->setValue($e, array(array()));
17+
var_dump($e->getTraceAsString());
18+
19+
echo "\nArray of array of NULL values:\n";
20+
$ref->setValue($e, array(array(
21+
'file' => NULL,
22+
'line' => NULL,
23+
'class' => NULL,
24+
'type' => NULL,
25+
'function' => NULL,
26+
'args' => NULL
27+
)));
28+
var_dump($e->getTraceAsString());
29+
?>
30+
--EXPECTF--
31+
Array of NULL:
32+
33+
Warning: Expected array for frame 0 in %s on line %d
34+
string(9) "#0 {main}"
35+
36+
Array of empty array:
37+
string(36) "#0 [internal function]: ()
38+
#1 {main}"
39+
40+
Array of array of NULL values:
41+
42+
Warning: Function name is no string in %s on line %d
43+
44+
Warning: Value for class is no string in %s on line %d
45+
46+
Warning: Value for type is no string in %s on line %d
47+
48+
Warning: Value for function is no string in %s on line %d
49+
50+
Warning: args element is no array in %s on line %d
51+
string(60) "#0 [unknown function][unknown][unknown][unknown]()
52+
#1 {main}"
53+

Zend/zend_exceptions.c

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,14 @@ ZEND_METHOD(error_exception, getSeverity)
350350
#define TRACE_APPEND_STR(val) \
351351
TRACE_APPEND_STRL(val, sizeof(val)-1)
352352

353-
#define TRACE_APPEND_KEY(key) \
354-
if (zend_hash_find(ht, key, sizeof(key), (void**)&tmp) == SUCCESS) { \
355-
TRACE_APPEND_STRL(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); \
353+
#define TRACE_APPEND_KEY(key) \
354+
if (zend_hash_find(ht, key, sizeof(key), (void**)&tmp) == SUCCESS) { \
355+
if (Z_TYPE_PP(tmp) != IS_STRING) { \
356+
zend_error(E_WARNING, "Value for %s is no string", key); \
357+
TRACE_APPEND_STR("[unknown]"); \
358+
} else { \
359+
TRACE_APPEND_STRL(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); \
360+
} \
356361
}
357362

358363
/* }}} */
@@ -461,6 +466,11 @@ static int _build_trace_string(zval **frame TSRMLS_DC, int num_args, va_list arg
461466
HashTable *ht = Z_ARRVAL_PP(frame);
462467
zval **file, **tmp;
463468

469+
if (Z_TYPE_PP(frame) != IS_ARRAY) {
470+
zend_error(E_WARNING, "Expected array for frame %lu", hash_key->h);
471+
return ZEND_HASH_APPLY_KEEP;
472+
}
473+
464474
str = va_arg(args, char**);
465475
len = va_arg(args, int*);
466476
num = va_arg(args, int*);
@@ -470,15 +480,25 @@ static int _build_trace_string(zval **frame TSRMLS_DC, int num_args, va_list arg
470480
TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
471481
efree(s_tmp);
472482
if (zend_hash_find(ht, "file", sizeof("file"), (void**)&file) == SUCCESS) {
473-
if (zend_hash_find(ht, "line", sizeof("line"), (void**)&tmp) == SUCCESS) {
474-
line = Z_LVAL_PP(tmp);
475-
} else {
476-
line = 0;
483+
if (Z_TYPE_PP(file) != IS_STRING) {
484+
zend_error(E_WARNING, "Function name is no string");
485+
TRACE_APPEND_STR("[unknown function]");
486+
} else{
487+
if (zend_hash_find(ht, "line", sizeof("line"), (void**)&tmp) == SUCCESS) {
488+
if (Z_TYPE_PP(tmp) == IS_LONG) {
489+
line = Z_LVAL_PP(tmp);
490+
} else {
491+
zend_error(E_WARNING, "Line is no long");
492+
line = 0;
493+
}
494+
} else {
495+
line = 0;
496+
}
497+
s_tmp = emalloc(Z_STRLEN_PP(file) + MAX_LENGTH_OF_LONG + 4 + 1);
498+
sprintf(s_tmp, "%s(%ld): ", Z_STRVAL_PP(file), line);
499+
TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
500+
efree(s_tmp);
477501
}
478-
s_tmp = emalloc(Z_STRLEN_PP(file) + MAX_LENGTH_OF_LONG + 4 + 1);
479-
sprintf(s_tmp, "%s(%ld): ", Z_STRVAL_PP(file), line);
480-
TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
481-
efree(s_tmp);
482502
} else {
483503
TRACE_APPEND_STR("[internal function]: ");
484504
}
@@ -487,10 +507,14 @@ static int _build_trace_string(zval **frame TSRMLS_DC, int num_args, va_list arg
487507
TRACE_APPEND_KEY("function");
488508
TRACE_APPEND_CHR('(');
489509
if (zend_hash_find(ht, "args", sizeof("args"), (void**)&tmp) == SUCCESS) {
490-
int last_len = *len;
491-
zend_hash_apply_with_arguments(Z_ARRVAL_PP(tmp) TSRMLS_CC, (apply_func_args_t)_build_trace_args, 2, str, len);
492-
if (last_len != *len) {
493-
*len -= 2; /* remove last ', ' */
510+
if (Z_TYPE_PP(tmp) == IS_ARRAY) {
511+
int last_len = *len;
512+
zend_hash_apply_with_arguments(Z_ARRVAL_PP(tmp) TSRMLS_CC, (apply_func_args_t)_build_trace_args, 2, str, len);
513+
if (last_len != *len) {
514+
*len -= 2; /* remove last ', ' */
515+
}
516+
} else {
517+
zend_error(E_WARNING, "args element is no array");
494518
}
495519
}
496520
TRACE_APPEND_STR(")\n");

0 commit comments

Comments
 (0)