Skip to content

Fix NUL byte in exception string terminating Exception::__toString() #10873

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions Zend/tests/gh10810.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
GH-10810: Fix NUL byte terminating Exception::__toString()
--FILE--
<?php
echo new \Exception("Hello\0World");
?>
--EXPECTF--
Exception: Hello%0World in %s:%d
Stack trace:
#0 {main}
22 changes: 11 additions & 11 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,24 +680,24 @@ 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 ")) {
zend_string *real_message = zend_strpprintf(0, "%s and defined", ZSTR_VAL(message));
zend_string *real_message = zend_strpprintf_unchecked(0, "%S and defined", message);
zend_string_release_ex(message, 0);
message = real_message;
}

zend_string *tmp_trace = (Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace))
? zend_string_copy(Z_STR(trace))
: ZSTR_INIT_LITERAL("#0 {main}\n", false);
if (ZSTR_LEN(message) > 0) {
str = zend_strpprintf(0, "%s: %s in %s:" ZEND_LONG_FMT
"\nStack trace:\n%s%s%s",
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(message), ZSTR_VAL(file), line,
(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
str = zend_strpprintf_unchecked(0, "%S: %S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
Z_OBJCE_P(exception)->name, message, file, line,
tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
} else {
str = zend_strpprintf(0, "%s in %s:" ZEND_LONG_FMT
"\nStack trace:\n%s%s%s",
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(file), line,
(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
str = zend_strpprintf_unchecked(0, "%S in %S:" ZEND_LONG_FMT "\nStack trace:\n%S%s%S",
Z_OBJCE_P(exception)->name, file, line,
tmp_trace, ZSTR_LEN(prev_str) ? "\n\nNext " : "", prev_str);
}
zend_string_release_ex(tmp_trace, false);

zend_string_release_ex(prev_str, 0);
zend_string_release_ex(message, 0);
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ END_EXTERN_C()

#define ZSTR_ALLOCA_FREE(str, use_heap) free_alloca(str, use_heap)

#define ZSTR_INIT_LITERAL(s, persistent) (zend_string_init((s), strlen(s), (persistent)))

/*---*/

static zend_always_inline zend_ulong zend_string_hash_val(zend_string *s)
Expand Down
9 changes: 9 additions & 0 deletions main/snprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,15 @@ static size_t format_converter(buffy * odp, const char *fmt, va_list ap) /* {{{
}
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:
Expand Down
9 changes: 9 additions & 0 deletions main/spprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,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:
Expand Down