Skip to content

Commit 139b99c

Browse files
committed
Fix phpGH-13097: Anonymous class reference in trigger_error / thrown Exception
1 parent 5e2a586 commit 139b99c

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

Zend/tests/gh13097_a.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-13097 (Anonymous class reference in trigger_error / thrown Exception)
3+
--FILE--
4+
<?php
5+
6+
$anonymous = new class(){};
7+
8+
trigger_error(
9+
get_class($anonymous).' ...now you don\'t!',
10+
E_USER_ERROR
11+
);
12+
13+
?>
14+
--EXPECTF--
15+
Fatal error: class@anonymous%s ...now you don't! in %s on line %d

Zend/tests/gh13097_b.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-13097 (Anonymous class reference in trigger_error / thrown Exception)
3+
--FILE--
4+
<?php
5+
6+
$anonymous = new class(){};
7+
8+
throw new Exception(
9+
get_class($anonymous).' ...now you don\'t!',
10+
E_USER_ERROR
11+
);
12+
13+
?>
14+
--EXPECTF--
15+
Fatal error: Uncaught Exception: class@anonymous%s ...now you don't! in %s:%d
16+
Stack trace:
17+
#0 {main}
18+
thrown in %s on line %d

Zend/zend_builtin_functions.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,10 +1108,9 @@ ZEND_FUNCTION(get_included_files)
11081108
ZEND_FUNCTION(trigger_error)
11091109
{
11101110
zend_long error_type = E_USER_NOTICE;
1111-
char *message;
1112-
size_t message_len;
1111+
zend_string *message;
11131112

1114-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &message, &message_len, &error_type) == FAILURE) {
1113+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &message, &error_type) == FAILURE) {
11151114
RETURN_THROWS();
11161115
}
11171116

@@ -1128,7 +1127,7 @@ ZEND_FUNCTION(trigger_error)
11281127
break;
11291128
}
11301129

1131-
zend_error((int)error_type, "%s", message);
1130+
zend_error_zstr_at(error_type, zend_get_executed_filename_ex(), zend_get_executed_lineno(), message);
11321131
// TODO Change to void
11331132
RETURN_TRUE;
11341133
}

Zend/zend_exceptions.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,10 @@ ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severit
950950
file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
951951
line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
952952

953+
ZVAL_STR(&tmp, str);
953954
zend_error_va(severity | E_DONT_BAIL,
954955
(file && ZSTR_LEN(file) > 0) ? file : NULL, line,
955-
"Uncaught %s\n thrown", ZSTR_VAL(str));
956+
"Uncaught %Z\n thrown", &tmp);
956957

957958
zend_string_release_ex(str, 0);
958959
zend_string_release_ex(file, 0);

main/main.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,19 +1350,25 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
13501350
php_printf("%s<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(buf), ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
13511351
zend_string_free(buf);
13521352
} else {
1353-
php_printf("%s<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
1353+
zval tmp;
1354+
ZVAL_STR(&tmp, message);
1355+
php_printf_unchecked("%s<br />\n<b>%s</b>: %Z in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, &tmp, ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
13541356
}
13551357
} else {
13561358
/* Write CLI/CGI errors to stderr if display_errors = "stderr" */
13571359
if ((!strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi") || !strcmp(sapi_module.name, "phpdbg")) &&
13581360
PG(display_errors) == PHP_DISPLAY_ERRORS_STDERR
13591361
) {
1360-
fprintf(stderr, "%s: %s in %s on line %" PRIu32 "\n", error_type_str, ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno);
1362+
fprintf(stderr, "%s: ", error_type_str);
1363+
fwrite(ZSTR_VAL(message), sizeof(char), ZSTR_LEN(message), stderr);
1364+
fprintf(stderr, " in %s on line %" PRIu32 "\n", ZSTR_VAL(error_filename), error_lineno);
13611365
#ifdef PHP_WIN32
13621366
fflush(stderr);
13631367
#endif
13641368
} else {
1365-
php_printf("%s\n%s: %s in %s on line %" PRIu32 "\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
1369+
zval tmp;
1370+
ZVAL_STR(&tmp, message);
1371+
php_printf_unchecked("%s\n%s: %Z in %s on line %" PRIu32 "\n%s", STR_PRINT(prepend_string), error_type_str, &tmp, ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
13661372
}
13671373
}
13681374
}

0 commit comments

Comments
 (0)