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

Truncated error messages produced by WinapiErrnoException #23191

Closed
TanninOne opened this issue Oct 1, 2018 · 1 comment · Fixed by #55207
Closed

Truncated error messages produced by WinapiErrnoException #23191

TanninOne opened this issue Oct 1, 2018 · 1 comment · Fixed by #55207
Labels
help wanted Issues that need assistance from volunteers or PRs that need help to proceed. windows Issues and PRs related to the Windows platform.

Comments

@TanninOne
Copy link

  • Version: 8.9.3 (from Electron 2.0.10)
  • Platform: windows
  • Subsystem: core

I'm writing a native extension and trying to throw exceptions using WinapiErrnoException but the error messages I get are truncated to only the first letter (so instead of "Access Denied" the error message is "A")

I believe I have tracked the error to https://github.com/nodejs/node/blob/master/src/exceptions.cc where it uses FormatMessage to generate a localised error message from an error code like this:

char* errmsg = nullptr;

 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
     FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, errorno,
     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, nullptr);

Now FormatMessage is just a macro that resolves to FormatMessageA on ansi builds and FormatMessageW on wide character/unicode builds. LPTSTR is also a macro that resolves to "char*" on ansi builds and "wchar_t*" on wide character builds.
So I can't be sure but I assume node is built with unicode support so the cast of char* to LPTSTR is invalid as the buffer produced by FormatMessage will be UTF-16 encoded.
This would explain why I get only the first character, as in an english text utf-16 encoded every second byte would be 0x00.

The simplest solution would be to swap FormatMessage for FormatMessageA to explicitly use the ansi variant but the correct solution (for proper localization) would be to change the function to actually use char*/wchar_t* appropriately and then convert to utf-8.

Now I will admit I feel a bit stupid writing this because I can't believe I'd be the first person to notice this if it was true so I'm sure someone will tell me how that's not it and the code actually works as intended - but I don't see how. At the very least the use of the cast from char* to LPTSTR is technically incorrect.

TanninOne added a commit to Nexus-Mods/node-winapi-bindings that referenced this issue Oct 1, 2018
this was caused by a bug in node presumably, see
nodejs/node#23191
@bzoz
Copy link
Contributor

bzoz commented Oct 1, 2018

/cc @nodejs/platform-windows

@jasnell jasnell added help wanted Issues that need assistance from volunteers or PRs that need help to proceed. windows Issues and PRs related to the Windows platform. labels Jun 26, 2020
huseyinacacak-janea added a commit to JaneaSystems/node that referenced this issue Oct 1, 2024
huseyinacacak-janea added a commit to JaneaSystems/node that referenced this issue Oct 1, 2024
huseyinacacak-janea added a commit to JaneaSystems/node that referenced this issue Oct 2, 2024
huseyinacacak-janea added a commit to JaneaSystems/node that referenced this issue Oct 14, 2024
aduh95 pushed a commit that referenced this issue Oct 19, 2024
louwers pushed a commit to louwers/node that referenced this issue Nov 2, 2024
tpoisseau pushed a commit to tpoisseau/node that referenced this issue Nov 21, 2024
ruyadorno pushed a commit that referenced this issue Nov 27, 2024
marco-ippolito pushed a commit that referenced this issue Jan 22, 2025
marco-ippolito pushed a commit that referenced this issue Jan 22, 2025
marco-ippolito pushed a commit that referenced this issue Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issues that need assistance from volunteers or PRs that need help to proceed. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants