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

Clarify Buffer hex string error message #4877

Closed
wants to merge 1 commit into from

Conversation

jgeewax
Copy link

@jgeewax jgeewax commented Jan 26, 2016

Taking a clue from https://github.com/nodejs/node/blob/master/src/string_bytes.cc#L503, provide more reason for why a hex string is invalid (namely that the issue is the length being odd rather than even).

@silverwind silverwind added the buffer Issues and PRs related to the buffer subsystem. label Jan 26, 2016
@bnoordhuis
Copy link
Member

I don't think we have a regression test for this. Can you add one to test/parallel/test-buffer.js?

Apropos the commit log, can you make sure it conforms to the guidelines from CONTRIBUTING.md? Thanks.

@mscdex mscdex added the c++ Issues and PRs that require attention from people who are familiar with C++. label Jan 26, 2016
@@ -623,7 +623,7 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
Local<String> str = args[0]->ToString(env->isolate());

if (encoding == HEX && str->Length() % 2 != 0)
return env->ThrowTypeError("Invalid hex string");
return env->ThrowTypeError("Invalid hex string length (must be even)");
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps, "The hex string length must be an even number" or "The hex string must have an even number of characters"

Copy link
Author

Choose a reason for hiding this comment

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

Done :)

@jasnell jasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Jan 27, 2016
@jasnell
Copy link
Member

jasnell commented Jan 27, 2016

Marking semver-major because of the error message change.

@jgeewax
Copy link
Author

jgeewax commented Jan 28, 2016

@bnoordhuis I added a simple test that looks for the TypeError and (new) message.

@cjihrig
Copy link
Contributor

cjihrig commented Jan 28, 2016

Can you run make lint on this, and correct the errors.

@@ -623,7 +623,8 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
Local<String> str = args[0]->ToString(env->isolate());

if (encoding == HEX && str->Length() % 2 != 0)
return env->ThrowTypeError("Invalid hex string");
return env->ThrowTypeError("Hex strings must have an even number of "
Copy link
Contributor

Choose a reason for hiding this comment

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

@bnoordhuis does our C++ style allow strings split like this (the linter doesn't complain)?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this should be fine.

@jgeewax
Copy link
Author

jgeewax commented Jan 28, 2016

@cjihrig Did a make lint and fixed anything outstanding. Let me know if you need anything else

@cjihrig
Copy link
Contributor

cjihrig commented Jan 28, 2016

The commit message should be buffer:, not buffers:, but whoever lands the commit can fix that up.

LGTM, but maybe @trevnorris wants to sign off on this?

@cjihrig
Copy link
Contributor

cjihrig commented Jan 28, 2016

@jgeewax
Copy link
Author

jgeewax commented Jan 28, 2016

@cjihrig : Fixed. I was going off the commit log, which seemed to have both. Sorry about that.

@cjihrig
Copy link
Contributor

cjihrig commented Jan 28, 2016

Looks like a commit just landed that conflicts with this one. Mind rebasing?

@jgeewax
Copy link
Author

jgeewax commented Jan 28, 2016

No problem. Let me know if that works for you.

@trevnorris
Copy link
Contributor

Is this the only location we throw for incorrect hex string length? If so then LGTM.

new Buffer('8', 'hex');
}, function(err) {
return err instanceof TypeError &&
err.message === 'The hex string must have an even number of characters';
Copy link
Member

Choose a reason for hiding this comment

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

minor nit: /^The hex string must have an even number of characters/.test(err.message)

@jasnell
Copy link
Member

jasnell commented Feb 1, 2016

LGTM with a couple of nits

@jasnell
Copy link
Member

jasnell commented Mar 22, 2016

@jgeewax ... ping. There were a few nits that need to be looked at.

@jgeewax jgeewax force-pushed the patch-1 branch 2 times, most recently from be3d46b to df3fe4f Compare March 27, 2016 16:14
@jgeewax
Copy link
Author

jgeewax commented Mar 27, 2016

OK -- Addressed the console.log stuff as well as the nit for regex checking vs equality.

@jasnell
Copy link
Member

jasnell commented Mar 27, 2016

LGTM

@@ -734,6 +734,14 @@ for (let i = 0; i < 256; i++) {
assert.equal(hexb2[i], hexb[i]);
}

// Create buffer from odd-length hex string should fail.
assert.throws(function() {
new Buffer('8', 'hex');
Copy link
Member

Choose a reason for hiding this comment

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

minor nit: can you update this to use Buffer.from('8', 'hex')

This was referenced Jul 7, 2023
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++. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.