Introduce %S modifier and use it (#13168)

This commit is contained in:
Niels Dossche 2024-01-16 22:51:02 +01:00 committed by GitHub
parent 055c8861cc
commit 8d5c3e6781
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 22 deletions

View File

@ -681,9 +681,7 @@ ZEND_METHOD(Exception, __toString)
}
if ((Z_OBJCE_P(exception) == zend_ce_type_error || Z_OBJCE_P(exception) == zend_ce_argument_count_error) && strstr(ZSTR_VAL(message), ", called in ")) {
zval message_zv;
ZVAL_STR(&message_zv, message);
zend_string *real_message = zend_strpprintf_unchecked(0, "%Z and defined", &message_zv);
zend_string *real_message = zend_strpprintf_unchecked(0, "%S and defined", message);
zend_string_release_ex(message, 0);
message = real_message;
}
@ -692,23 +690,19 @@ ZEND_METHOD(Exception, __toString)
? zend_string_copy(Z_STR(trace))
: ZSTR_INIT_LITERAL("#0 {main}\n", false);
zval name_zv, trace_zv, file_zv, prev_str_zv;
ZVAL_STR(&name_zv, Z_OBJCE_P(exception)->name);
ZVAL_STR(&trace_zv, tmp_trace);
ZVAL_STR(&file_zv, file);
ZVAL_STR(&prev_str_zv, prev_str);
zend_string *name = Z_OBJCE_P(exception)->name;
if (ZSTR_LEN(message) > 0) {
zval message_zv;
ZVAL_STR(&message_zv, message);
str = zend_strpprintf_unchecked(0, "%Z: %Z in %Z:" ZEND_LONG_FMT "\nStack trace:\n%Z%s%Z",
&name_zv, &message_zv, &file_zv, line,
&trace_zv, ZSTR_LEN(prev_str) ? "\n\nNext " : "", &prev_str_zv);
str = zend_strpprintf_unchecked(0, "%S: %S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
name, message, file, line,
tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
} else {
str = zend_strpprintf_unchecked(0, "%Z in %Z:" ZEND_LONG_FMT "\nStack trace:\n%Z%s%Z",
&name_zv, &file_zv, line,
&trace_zv, ZSTR_LEN(prev_str) ? "\n\nNext " : "", &prev_str_zv);
str = zend_strpprintf_unchecked(0, "%S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
name, file, line,
tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
}
zend_string_release_ex(tmp_trace, false);
@ -951,10 +945,9 @@ ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severit
file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
ZVAL_STR(&tmp, str);
zend_error_va(severity | E_DONT_BAIL,
(file && ZSTR_LEN(file) > 0) ? file : NULL, line,
"Uncaught %Z\n thrown", &tmp);
"Uncaught %S\n thrown", str);
zend_string_release_ex(str, 0);
zend_string_release_ex(file, 0);

View File

@ -1361,9 +1361,7 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
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));
zend_string_free(buf);
} else {
zval tmp;
ZVAL_STR(&tmp, message);
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));
php_printf_unchecked("%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, message, ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
}
} else {
/* Write CLI/CGI errors to stderr if display_errors = "stderr" */
@ -1377,9 +1375,7 @@ static ZEND_COLD void php_error_cb(int orig_type, zend_string *error_filename, c
fflush(stderr);
#endif
} else {
zval tmp;
ZVAL_STR(&tmp, message);
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));
php_printf_unchecked("%s\n%s: %S in %s on line %" PRIu32 "\n%s", STR_PRINT(prepend_string), error_type_str, message, ZSTR_VAL(error_filename), error_lineno, STR_PRINT(append_string));
}
}
}

View File

@ -371,6 +371,15 @@ static void xbuf_format_converter(void *xbuf, bool is_char, const char *fmt, va_
}
break;
}
case 'S': {
zend_string *str = va_arg(ap, zend_string*);
s_len = ZSTR_LEN(str);
s = ZSTR_VAL(str);
if (adjust_precision && (size_t)precision < s_len) {
s_len = precision;
}
break;
}
case 'u':
switch(modifier) {
default: