Skip to content

Commit 764360b

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: Fix phpGH-13097: Anonymous class reference in trigger_error / thrown Exception
2 parents 83c8d02 + 2cde4b2 commit 764360b

File tree

6 files changed

+49
-8
lines changed

6 files changed

+49
-8
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ PHP NEWS
55
- Core:
66
. Fixed timer leak in zend-max-execution-timers builds. (withinboredom)
77
. Fixed bug GH-12349 (linking failure on ARM with mold). (Jan Palus)
8+
. Fixed bug GH-13097 (Anonymous class reference in trigger_error / thrown
9+
Exception). (nielsdos)
810

911
- Curl:
1012
. Fix missing error check in curl_multi_init(). (divinity76)

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
@@ -1123,10 +1123,9 @@ ZEND_FUNCTION(get_included_files)
11231123
ZEND_FUNCTION(trigger_error)
11241124
{
11251125
zend_long error_type = E_USER_NOTICE;
1126-
char *message;
1127-
size_t message_len;
1126+
zend_string *message;
11281127

1129-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &message, &message_len, &error_type) == FAILURE) {
1128+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &message, &error_type) == FAILURE) {
11301129
RETURN_THROWS();
11311130
}
11321131

@@ -1143,7 +1142,7 @@ ZEND_FUNCTION(trigger_error)
11431142
break;
11441143
}
11451144

1146-
zend_error((int)error_type, "%s", message);
1145+
zend_error_zstr_at(error_type, zend_get_executed_filename_ex(), zend_get_executed_lineno(), message);
11471146
// TODO Change to void
11481147
RETURN_TRUE;
11491148
}

Zend/zend_exceptions.c

Lines changed: 2 additions & 1 deletion
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

Lines changed: 9 additions & 3 deletions
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)