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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,8 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
enc == UCS2 ? str_obj->Length() * sizeof(uint16_t) : str_obj->Length();

if (enc == HEX && str_length % 2 != 0)
return env->ThrowTypeError("Invalid hex string");
return env->ThrowTypeError("Hex strings must have an even number of "
"characters");

if (str_length == 0)
return;
Expand Down Expand Up @@ -664,7 +665,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.

"characters");

size_t offset;
size_t max_length;
Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ assert.throws(() => Buffer.from('A', 'hex'), TypeError);
// Test single base64 char encodes as 0
assert.strictEqual(Buffer.from('A', 'base64').length, 0);

// Creating a buffer from odd-length hex string should fail.
assert.throws(() => Buffer.from('zzz', 'hex'),
/Hex strings must have an even number of characters/);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you make this check stricter by adding ^, $, and the error type.


{
// test an invalid slice end.
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-buffer-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ testBufs('61c8b462c8b563c8b6', 12, 1, 'hex');
// Make sure this operation doesn't go on forever
buf1.fill('yKJh', 'hex');
assert.throws(() =>
buf1.fill('\u0222', 'hex'), /^TypeError: Invalid hex string$/);
buf1.fill('\u0222', 'hex'), /^TypeError:/);
Copy link
Member

Choose a reason for hiding this comment

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

Can you check the error message here?



// BASE64
Expand Down