The Wayback Machine - https://web.archive.org/web/20220424070318/https://github.com/nodejs/node/pull/7764
Skip to content
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

src: don't include a null character in the WriteConsoleW call #7764

Closed
wants to merge 3 commits into from

Conversation

Copy link
Contributor

@seishun seishun commented Jul 16, 2016

Checklist
  • make -j4 test (UNIX), or vcbuild test nosign (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

It seems if WriteConsoleW is called with a string that has a null character at the end, the console turns it into a space.

Fixes: #7755

@nodejs-github-bot nodejs-github-bot added the c++ label Jul 16, 2016
@cjihrig
Copy link
Contributor

@cjihrig cjihrig commented Jul 16, 2016

Could you add a regression test for this based on #7755?

@seishun
Copy link
Contributor Author

@seishun seishun commented Jul 16, 2016

Not sure how. I'm unable to reproduce this issue when stderr is piped. It seems to only occur when it's written directly to a console.

@addaleax addaleax added the windows label Jul 16, 2016
@addaleax
Copy link
Member

@addaleax addaleax commented Jul 16, 2016

It might be nice to know why node is trying to print an error message in the first place, maybe that also answers why it doesn’t occur with piped stderr?

@seishun
Copy link
Contributor Author

@seishun seishun commented Jul 16, 2016

No, it does still print an error message with piped stderr. It just doesn't have anything after the trailing newline in that case.

The "error message" is "Debugger listening on [::]:5858" for example.

@@ -251,7 +251,7 @@ static void PrintErrorString(const char* format, ...) {
vsprintf(out.data(), format, ap);

// Get required wide buffer size
n = MultiByteToWideChar(CP_UTF8, 0, out.data(), -1, nullptr, 0);
n = MultiByteToWideChar(CP_UTF8, 0, out.data(), n, nullptr, 0);
Copy link
Member

@bnoordhuis bnoordhuis Jul 17, 2016

Choose a reason for hiding this comment

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

I don't think this works, it's going to fail when n == 0. (Also, passing n here but -1 three lines below doesn't look Obviously Correct to me.)

I think the patch should look something like this, using explicit sizes everywhere.

diff --git a/src/node.cc b/src/node.cc
index 3998659..1f0ba27 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -246,16 +246,19 @@ static void PrintErrorString(const char* format, ...) {
   }

   // Fill in any placeholders
-  int n = _vscprintf(format, ap);
-  std::vector<char> out(n + 1);
-  vsprintf(out.data(), format, ap);
+  const int numbytes = _vscprintf(format, ap);
+  if (numbytes <= 0) return;
+  std::vector<char> bytes(numbytes + 1);
+  vsprintf(bytes.data(), format, ap);

   // Get required wide buffer size
-  n = MultiByteToWideChar(CP_UTF8, 0, out.data(), -1, nullptr, 0);
-
-  std::vector<wchar_t> wbuf(n);
-  MultiByteToWideChar(CP_UTF8, 0, out.data(), -1, wbuf.data(), n);
-  WriteConsoleW(stderr_handle, wbuf.data(), n, nullptr, nullptr);
+  const int numchars =
+      MultiByteToWideChar(CP_UTF8, 0, bytes.data(), numbytes, nullptr, 0);
+  std::vector<wchar_t> chars(numchars);
+  MultiByteToWideChar(CP_UTF8, 0,
+                      bytes.data(), numbytes,
+                      chars.data(), numchars);
+  WriteConsoleW(stderr_handle, chars.data(), numchars, nullptr, nullptr);
 #else
   vfprintf(stderr, format, ap);
 #endif

Copy link
Contributor Author

@seishun seishun Jul 17, 2016

Choose a reason for hiding this comment

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

OK, agreed, I'll make it simpler. (I wanted to avoid allocating for the unused wide null character, but it's not worth it if it makes the code more complicated)

@cjihrig
Copy link
Contributor

@cjihrig cjihrig commented Jul 18, 2016

@seishun regarding the regression test, @Fishrock123 recently added better support for TTY testing (see /test/pseudo-tty/). Did you happen to try adding a TTY test?

@addaleax
Copy link
Member

@addaleax addaleax commented Jul 18, 2016

I think these tests are POSIX-only right now, unfortunately.

@Fishrock123
Copy link
Member

@Fishrock123 Fishrock123 commented Jul 18, 2016

@cjihrig Pseudo-terminals from python are not available on windows.

We could start using a shim like https://www.npmjs.com/package/pty.js uses. Maybe we should wrap it in a Node shim that uses that module? Or maybe someone with better python knowledge could duplicate the pty.js shim for Windows?

@seishun
Copy link
Contributor Author

@seishun seishun commented Jul 18, 2016

Using a terminal emulator wouldn't really make sense for testing this, since the issue is caused by a peculiarity of the Windows console. For instance, this issue doesn't happen in the MSYS2 shell.

Perhaps we could run the test through winpty. If we could somehow capture its output, we could test whether there are any unnecessary spaces on the second line.

@jasnell
Copy link
Member

@jasnell jasnell commented Aug 8, 2016

LGTM.. but it is unfortunate that there does not appear to be a reliable regression test for this.

@jasnell
Copy link
Member

@jasnell jasnell commented Aug 18, 2016

@nodejs/ctc @nodejs/platform-windows ... ping... any further thoughts on this one?

@bnoordhuis
Copy link
Member

@bnoordhuis bnoordhuis commented Aug 18, 2016

There should probably be a CHECK_GT(n, 0) and the comment should be punctuated, otherwise LGTM.

@seishun
Copy link
Contributor Author

@seishun seishun commented Aug 21, 2016

There should probably be a CHECK_GT(n, 0)

Where exactly? n can't be 0 here.

the comment should be punctuated, otherwise LGTM

The other comments in this function aren't punctuated either.

@addaleax
Copy link
Member

@addaleax addaleax commented Aug 22, 2016

There should probably be a CHECK_GT(n, 0)

Where exactly? n can't be 0 here.

Just before WriteConsoleW. And yes, that CHECK_GT is pretty much supposed to make sure that you’re always right here. ;)

@seishun
Copy link
Contributor Author

@seishun seishun commented Aug 23, 2016

Added the check.

@jasnell
Copy link
Member

@jasnell jasnell commented Aug 23, 2016

LGTM

@jasnell
Copy link
Member

@jasnell jasnell commented Aug 23, 2016

jasnell pushed a commit that referenced this issue Aug 23, 2016
Fixes: #7755
PR-URL: #7764
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
@jasnell
Copy link
Member

@jasnell jasnell commented Aug 23, 2016

Landed in 09f861f

@jasnell jasnell closed this Aug 23, 2016
evanlucas pushed a commit that referenced this issue Aug 24, 2016
Fixes: #7755
PR-URL: #7764
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
@MylesBorins
Copy link
Member

@MylesBorins MylesBorins commented Sep 30, 2016

@seishun should this be backported?

@seishun
Copy link
Contributor Author

@seishun seishun commented Sep 30, 2016

@thealphanerd If it applies cleanly to a branch, then that branch is affected.

MylesBorins pushed a commit that referenced this issue Sep 30, 2016
Fixes: #7755
PR-URL: #7764
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
MylesBorins pushed a commit that referenced this issue Sep 30, 2016
Fixes: #7755
PR-URL: #7764
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
MylesBorins pushed a commit that referenced this issue Oct 10, 2016
Fixes: #7755
PR-URL: #7764
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
rvagg pushed a commit that referenced this issue Oct 18, 2016
Fixes: #7755
PR-URL: #7764
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
MylesBorins pushed a commit that referenced this issue Oct 26, 2016
Fixes: #7755
PR-URL: #7764
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
@MylesBorins MylesBorins mentioned this pull request Oct 26, 2016
@seishun seishun deleted the debug branch Jun 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ windows
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.