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: do proper StringBytes error handling #12765

Closed
wants to merge 4 commits into from

Conversation

addaleax
Copy link
Member

@addaleax addaleax commented Apr 30, 2017

  • Return MaybeLocals from StringBytes::Encode
  • Add an error out parameter to pass JS exceptions to the callers (instead of directly throwing)
  • Simplify some of the string generation methods in string_bytes.cc by unifying the EXTERN_APEX logic
  • Reduce usage of deprecated V8 APIs.
  • Remove error handling logic from JS, the buffer.*Slice() methods now throw errors themselves.
  • Left TODO comments for future semver-major error message improvements.

This paves the way for better error messages coming out of the StringBytes methods.

Ref: #3175

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines
Affected core subsystem(s)

buffer, fs, os, crypto


CI: https://ci.nodejs.org/job/node-test-commit/9528/

Maybe @TimothyGu or @tniessen would want to review?

Also, /cc @MylesBorins , if you’re still interested in #3175 … that should become a lot easier after this :)

@addaleax addaleax added buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. fs Issues and PRs related to the fs subsystem / file system. lib / src Issues and PRs related to general changes in the lib or src directory. lts-watch-v6.x os Issues and PRs related to the os subsystem. labels Apr 30, 2017
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels Apr 30, 2017
lib/buffer.js Outdated
@@ -590,11 +589,8 @@ Buffer.prototype.toString = function(encoding, start, end) {

if (end <= start)
Copy link
Member

Choose a reason for hiding this comment

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

This is much clearer now, had to double check undefined <= undefined returns false due to number hint.

Copy link
Member Author

Choose a reason for hiding this comment

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

@benjamingr Not sure, what are you saying? 😄 I didn’t really touch this part of the logic ;)

Copy link
Member

Choose a reason for hiding this comment

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

You added an early return which eliminates a correct (but very tricky) behavior in the old code where it would check if end <= start for undefined values. I'm just saying I'm happy with the change :)

delete[] md_value;
args.GetReturnValue().Set(rc);
// TODO(addaleax): Throw `error` here. This isn't so terribly important,
Copy link
Member

Choose a reason for hiding this comment

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

So why not throw the error here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, right, I could just do that here. I didn’t want to accidentally turn this into a semver-major change, but I guess here it really doesn’t make a difference… Updated!

MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
const uint16_t* buf,
size_t buflen,
Local<Value>* error) {
Copy link
Member

@benjamingr benjamingr Apr 30, 2017

Choose a reason for hiding this comment

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

Personally, I would use a reference and not a pointer here - passing Isolate as a pointer makes sense since it's convention - but if the goal is to literally a const pointer to an object that might change (but not the pointer) I think a reference is cleaner. If this is convention and we can't do a reference - can we make it a const pointer?

(goes to all sites)

Copy link
Member Author

Choose a reason for hiding this comment

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

Personally, I agree. But yes, we don’t use non-const references in this codebase, and I can live with that… ¯\_(ツ)_/¯

If this is convention - can we make it a const pointer?

Sorry, which thing should be made const? The Local can’t be const, Encode() needs to be able to assign to it. The pointer itself could be const, but it’s a pass-by-value function parameter, so that doesn’t really matter anyway, does it?

Copy link
Member

Choose a reason for hiding this comment

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

The pointer itself, so it's clear it doesn't change - but I don't feel strongly about this and it's plenty obvious it won't change (like the isolate).


case BASE64: {
size_t dlen = base64_encoded_size(buflen);
char* dst = node::UncheckedMalloc(dlen);
if (dst == nullptr) {
return Local<String>();
goto malloc_failed;
Copy link
Member

Choose a reason for hiding this comment

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

I think a function containing the logic of malloc_failed and call sites calling the functions (rather than using goto is cleaner. I don't have a lot of experience with this code though.

Copy link
Member Author

Choose a reason for hiding this comment

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

I’ve just inlined the malloc_failed case, it was a lot bulkier and made more sense this way when I started editing. 😄

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

I have several nits:

  • Lots of new TODOs that can probably be fixed in this PR pretty quickly.
  • I think a reference is cleaner (or at least a const pointer)
  • Not a goto fan.

That said, I gave the code a read, understand what it does and the changes look good to me with or without the nits addressed - given we can always address them later nits addressed.

I'd feel more comfortable if someone more experienced with the code gave this a review too though :)

@addaleax
Copy link
Member Author

Lots of new TODOs that can probably be fixed in this PR pretty quickly.

I’d like to keep them for a quick semver-major follow-up PR after this lands, if that’s okay.

@benjamingr
Copy link
Member

@addaleax sounds good to me. Editing previous comment.

lib/buffer.js Outdated
if (arguments.length === 0) {
result = this.utf8Slice(0, this.length);
return this.utf8Slice(0, this.length);
Copy link
Contributor

Choose a reason for hiding this comment

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

By changing this to return, you can drop the else and save a level of indentation on the rest of the code below.

Copy link
Member Author

Choose a reason for hiding this comment

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

@cjihrig yup, done!

- Return `MaybeLocal`s from `StringBytes::Encode`
- Add an `error` out parameter to pass JS exceptions to the callers
  (instead of directly throwing)
- Simplify some of the string generation methods in `string_bytes.cc`
  by unifying the `EXTERN_APEX` logic
- Reduce usage of deprecated V8 APIs.
- Remove error handling logic from JS, the `buffer.*Slice()` methods
  now throw errors themselves.
- Left TODO comments for future semver-major error message
  improvements.

This paves the way for better error messages coming out of the
StringBytes methods.

Ref: nodejs#3175
@tniessen
Copy link
Member

tniessen commented May 1, 2017

Nothing to add, looks good :)

@addaleax
Copy link
Member Author

addaleax commented May 3, 2017

@addaleax
Copy link
Member Author

addaleax commented May 3, 2017

Landed in d56a7e6

@addaleax addaleax closed this May 3, 2017
@addaleax addaleax deleted the stringbytes-cleanup branch May 3, 2017 17:23
addaleax added a commit that referenced this pull request May 3, 2017
- Return `MaybeLocal`s from `StringBytes::Encode`
- Add an `error` out parameter to pass JS exceptions to the callers
  (instead of directly throwing)
- Simplify some of the string generation methods in `string_bytes.cc`
  by unifying the `EXTERN_APEX` logic
- Reduce usage of deprecated V8 APIs.
- Remove error handling logic from JS, the `buffer.*Slice()` methods
  now throw errors themselves.
- Left TODO comments for future semver-major error message
  improvements.

This paves the way for better error messages coming out of the
StringBytes methods.

Ref: #3175
PR-URL: #12765
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
anchnk pushed a commit to anchnk/node that referenced this pull request May 6, 2017
- Return `MaybeLocal`s from `StringBytes::Encode`
- Add an `error` out parameter to pass JS exceptions to the callers
  (instead of directly throwing)
- Simplify some of the string generation methods in `string_bytes.cc`
  by unifying the `EXTERN_APEX` logic
- Reduce usage of deprecated V8 APIs.
- Remove error handling logic from JS, the `buffer.*Slice()` methods
  now throw errors themselves.
- Left TODO comments for future semver-major error message
  improvements.

This paves the way for better error messages coming out of the
StringBytes methods.

Ref: nodejs#3175
PR-URL: nodejs#12765
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
@gibfahn
Copy link
Member

gibfahn commented Jun 18, 2017

Should this be backported to v6.x-staging? If yes please follow the guide and raise a backport PR, if no let me know or add the dont-land-on label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. fs Issues and PRs related to the fs subsystem / file system. lib / src Issues and PRs related to general changes in the lib or src directory. os Issues and PRs related to the os subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants