Skip to content

Introduce %S modifier and use it #13168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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" */
Expand All @@ -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));
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions main/spprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
Copy link
Member Author

@nielsdos nielsdos Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could in principle use goto, after fetching the zend_string, to a part of the 'Z' case, but idk I didn't want to impede readability as we don't typically use goto unless in error situations.

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:
Expand Down