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
Closed

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

wants to merge 3 commits into from

Conversation

seishun
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++ Issues and PRs that require attention from people who are familiar with C++. label Jul 16, 2016
@cjihrig
Copy link
Contributor

cjihrig commented Jul 16, 2016

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

@seishun
Copy link
Contributor Author

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 Issues and PRs related to the Windows platform. label Jul 16, 2016
@addaleax
Copy link
Member

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 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

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

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 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

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

@Fishrock123
Copy link
Contributor

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 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 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 commented Aug 18, 2016

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

@bnoordhuis
Copy link
Member

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

@seishun
Copy link
Contributor Author

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

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 commented Aug 23, 2016

Added the check.

@jasnell
Copy link
Member

jasnell commented Aug 23, 2016

LGTM

@jasnell
Copy link
Member

jasnell commented Aug 23, 2016

jasnell pushed a commit that referenced this pull request 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 commented Aug 23, 2016

Landed in 09f861f

@jasnell jasnell closed this Aug 23, 2016
evanlucas pushed a commit that referenced this pull request 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
Contributor

@seishun should this be backported?

@seishun
Copy link
Contributor Author

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 pull request 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 pull request 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 pull request 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 pull request 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 pull request 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 June 10, 2017 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

If node is started with --debug a space appears on the following line
8 participants