Skip to content

Commit 836cc98

Browse files
authored
Prevent format injection in hosting Windows PAL printf functions when redirected to file (#119786)
1 parent ec5d36b commit 836cc98

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/native/corehost/hostmisc/pal.windows.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ void pal::file_vprintf(FILE* f, const pal::char_t* format, va_list vl)
2222
}
2323

2424
namespace {
25+
void file_printf(FILE* fallbackFileHandle, const pal::char_t* format, ...)
26+
{
27+
va_list args;
28+
va_start(args, format);
29+
pal::file_vprintf(fallbackFileHandle, format, args);
30+
va_end(args);
31+
}
32+
2533
void print_line_to_handle(const pal::char_t* message, HANDLE handle, FILE* fallbackFileHandle) {
2634
// String functions like vfwprintf convert wide to multi-byte characters as if wcrtomb were called - that is, using the current C locale (LC_TYPE).
2735
// In order to properly print UTF-8 and GB18030 characters to the console without requiring the user to use chcp to a compatible locale, we use WriteConsoleW.
@@ -33,7 +41,7 @@ namespace {
3341
{
3442
// We use file_vprintf to handle UTF-8 formatting. The WriteFile api will output the bytes directly with Unicode bytes,
3543
// while pal::file_vprintf will convert the characters to UTF-8.
36-
pal::file_vprintf(fallbackFileHandle, message, va_list());
44+
file_printf(fallbackFileHandle, _X("%s"), message);
3745
}
3846
else {
3947
::WriteConsoleW(handle, message, (int)pal::strlen(message), NULL, NULL);

0 commit comments

Comments
 (0)