Skip to content

Make Nan::ThrowError calls consistent and clean up when errors are thrown - #103

Merged
Daniel Imms (Tyriar) merged 6 commits into
masterfrom
102_throwerror_inconsistent
Jul 9, 2017
Merged

Daniel Imms (Tyriar) merged 6 commits into
masterfrom
102_throwerror_inconsistent

Conversation

@Tyriar

Copy link
Copy Markdown
Contributor

Fixes #102


Ryan Prichard (@rprichard) if you can a quick review would be awesome

@Tyriar Daniel Imms (Tyriar) added this to the 0.6.9 milestone Jul 7, 2017
@Tyriar Daniel Imms (Tyriar) self-assigned this Jul 7, 2017

@rprichard Ryan Prichard (rprichard) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm traveling, so I looked it over quickly. It basically looked OK to me. I noticed a few things that may need fixing.

Comment thread src/win/pty.cc Outdated
std::stringstream why;
std::wstring msg(winpty_error_msg(error_ptr));
std::string msg_(msg.begin(), msg.end());
std::string generalMsg_(generalMsg);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like generalMsg_ is unused?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Comment thread src/win/pty.cc
void throw_winpty_error(const char *generalMsg, winpty_error_ptr_t error_ptr) {
std::stringstream why;
std::wstring msg(winpty_error_msg(error_ptr));
std::string msg_(msg.begin(), msg.end());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not 100% sure, but I think the initializer of msg_ converts the wide string to a narrow string by truncating each wchar_t value to a char. It will only be correct for ASCII characters. I'm guessing node.js and/or v8 want UTF-8 strings? Standard C++ has a way to do that conversion (as of C++11, I think -- I'm thinking of std::codecvt), and maybe node.js / v8 also does? Windows has C-style APIs to do the conversion.

I think the pattern already existed in the code, so it's probably not necessary to fix it in this PR? e.g. see shellpath_'s initializer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback, tracking in #104 🙂

Comment thread src/win/pty.cc
throw_winpty_error("Error creating WinPTY config", error_ptr);
goto cleanup;
}
winpty_error_free(error_ptr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the code leaks error objects when an error occurs. Should there should be another winpty_error_free call for the cleanup label? (If you add one, make sure you don't call winpty_error_free twice on the same non-nullptr value -- that would cause a double-free crash. Maybe set error_ptr to nullptr after each free call.)

The other calls could be removed, in practice, because each winpty API either:

  • succeeds, and sets *error_ptr to nullptr, OR
  • fails, and sets *error_ptr to an error object.

Calling winpty_error_free on nullptr has no effect. I'm not sure whether the best convention is to always call winpty_error_free after each API call, or whether to only call it after detecting an error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@Tyriar
Daniel Imms (Tyriar) merged commit 9059772 into master Jul 9, 2017
@Tyriar
Daniel Imms (Tyriar) deleted the 102_throwerror_inconsistent branch July 9, 2017 19:04
Daniel Imms (Tyriar) added a commit that referenced this pull request Jul 9, 2017
When winpty exceptions occurred they were silently failing due
to marshal not being initialized.

Part of #102
Part of #103
xiaobaidadada pushed a commit to xiaobaidadada/node-pty-prebuilt that referenced this pull request Feb 25, 2025
Make Nan::ThrowError calls consistent and clean up when errors are thrown
xiaobaidadada pushed a commit to xiaobaidadada/node-pty-prebuilt that referenced this pull request Feb 25, 2025
When winpty exceptions occurred they were silently failing due
to marshal not being initialized.

Part of microsoft#102
Part of microsoft#103
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants