Skip to content

Commit 5e19c4a

Browse files
lundibundicodebytere
authored andcommitted
src: fix console debug output on Windows
The FWrite function on Windows assumed that MultiByteToWideChar automatically null-terminates the resulting string, but it will only do so if the size of the source was passed as -1 or null character was explicitly counted in the size. The FWrite uses std::string and its `.size()` method which doesn't count the null character even though the `.data()` method adds it to the resulting string. https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar#remarks > MultiByteToWideChar does not null-terminate an output string if the input string length is explicitly specified without a terminating null character. To null-terminate an output string for this function, the application should pass in -1 or explicitly count the terminating null character for the input string. PR-URL: #31580 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 0eb2dbb commit 5e19c4a

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

src/debug_utils.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,7 @@ void FWrite(FILE* file, const std::string& str) {
468468
std::vector<wchar_t> wbuf(n);
469469
MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), wbuf.data(), n);
470470

471-
// Don't include the final null character in the output
472-
CHECK_GT(n, 0);
473-
WriteConsoleW(handle, wbuf.data(), n - 1, nullptr, nullptr);
471+
WriteConsoleW(handle, wbuf.data(), n, nullptr, nullptr);
474472
return;
475473
#elif defined(__ANDROID__)
476474
if (file == stderr) {

test/parallel/test-http2-debug.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ const { stdout, stderr } = child_process.spawnSync(process.execPath, [
1212
path.resolve(__dirname, 'test-http2-ping.js')
1313
], { encoding: 'utf8' });
1414

15-
assert(stderr.match(/Setting the NODE_DEBUG environment variable to 'http2' can expose sensitive data \(such as passwords, tokens and authentication headers\) in the resulting log\./),
15+
assert(stderr.match(/Setting the NODE_DEBUG environment variable to 'http2' can expose sensitive data \(such as passwords, tokens and authentication headers\) in the resulting log\.\r?\n/),
1616
stderr);
17-
assert(stderr.match(/Http2Session client \(\d+\) handling data frame for stream \d+/),
17+
assert(stderr.match(/Http2Session client \(\d+\) handling data frame for stream \d+\r?\n/),
1818
stderr);
19-
assert(stderr.match(/HttpStream \d+ \(\d+\) \[Http2Session client \(\d+\)\] reading starting/),
19+
assert(stderr.match(/HttpStream \d+ \(\d+\) \[Http2Session client \(\d+\)\] reading starting\r?\n/),
2020
stderr);
21-
assert(stderr.match(/HttpStream \d+ \(\d+\) \[Http2Session client \(\d+\)\] closed with code 0/),
21+
assert(stderr.match(/HttpStream \d+ \(\d+\) \[Http2Session client \(\d+\)\] closed with code 0\r?\n/),
2222
stderr);
23-
assert(stderr.match(/HttpStream \d+ \(\d+\) \[Http2Session server \(\d+\)\] closed with code 0/),
23+
assert(stderr.match(/HttpStream \d+ \(\d+\) \[Http2Session server \(\d+\)\] closed with code 0\r?\n/),
2424
stderr);
25-
assert(stderr.match(/HttpStream \d+ \(\d+\) \[Http2Session server \(\d+\)\] tearing down stream/),
25+
assert(stderr.match(/HttpStream \d+ \(\d+\) \[Http2Session server \(\d+\)\] tearing down stream\r?\n/),
2626
stderr);
2727
assert.strictEqual(stdout.trim(), '');

0 commit comments

Comments
 (0)