Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
buffer: improve error message for uneven input
When creating a buffer with an uneven length string, the error message
is unclear about why the input is invalid. The check is looking
specifically for whether the input was an even number of characters and
should be clear about that.
  • Loading branch information
jgeewax committed Mar 2, 2017
commit 6e31164f3b9bd9312624efca987175ca0d04d099
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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