Skip to content

Commit 4091d24

Browse files
committed
Merge branch 'PHP-8.3'
* PHP-8.3: Fix GH-13097: Anonymous class reference in trigger_error / thrown Exception
2 parents 92d53c8 + 764360b commit 4091d24

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

Zend/tests/gh13097_a.phpt

+15
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

+18
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

+3-4
Original file line numberDiff line numberDiff line change
@@ -1127,10 +1127,9 @@ ZEND_FUNCTION(get_included_files)
11271127
ZEND_FUNCTION(trigger_error)
11281128
{
11291129
zend_long error_type = E_USER_NOTICE;
1130-
char *message;
1131-
size_t message_len;
1130+
zend_string *message;
11321131

1133-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &message, &message_len, &error_type) == FAILURE) {
1132+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &message, &error_type) == FAILURE) {
11341133
RETURN_THROWS();
11351134
}
11361135

@@ -1147,7 +1146,7 @@ ZEND_FUNCTION(trigger_error)
11471146
break;
11481147
}
11491148

1150-
zend_error((int)error_type, "%s", message);
1149+
zend_error_zstr_at(error_type, zend_get_executed_filename_ex(), zend_get_executed_lineno(), message);
11511150
// TODO Change to void
11521151
RETURN_TRUE;
11531152
}

Zend/zend_exceptions.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,10 @@ ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severit
951951
file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
952952
line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
953953

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

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

main/main.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -1361,19 +1361,25 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
13611361
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));
13621362
zend_string_free(buf);
13631363
} else {
1364-
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));
1364+
zval tmp;
1365+
ZVAL_STR(&tmp, message);
1366+
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));
13651367
}
13661368
} else {
13671369
/* Write CLI/CGI errors to stderr if display_errors = "stderr" */
13681370
if ((!strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi") || !strcmp(sapi_module.name, "phpdbg")) &&
13691371
PG(display_errors) == PHP_DISPLAY_ERRORS_STDERR
13701372
) {
1371-
fprintf(stderr, "%s: %s in %s on line %" PRIu32 "\n", error_type_str, ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno);
1373+
fprintf(stderr, "%s: ", error_type_str);
1374+
fwrite(ZSTR_VAL(message), sizeof(char), ZSTR_LEN(message), stderr);
1375+
fprintf(stderr, " in %s on line %" PRIu32 "\n", ZSTR_VAL(error_filename), error_lineno);
13721376
#ifdef PHP_WIN32
13731377
fflush(stderr);
13741378
#endif
13751379
} else {
1376-
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));
1380+
zval tmp;
1381+
ZVAL_STR(&tmp, message);
1382+
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));
13771383
}
13781384
}
13791385
}

0 commit comments

Comments
 (0)