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

Buffer bug fix #6775

Closed
wants to merge 6 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ function fromString(string, encoding) {
if (!Buffer.isEncoding(encoding))
throw new TypeError('"encoding" must be a valid string encoding');

var length = byteLength(string, encoding);

if (length === 0)
if (string.length === 0)
return Buffer.alloc(0);

var length = byteLength(string, encoding);

if (length >= (Buffer.poolSize >>> 1))
return binding.createFromString(string, encoding);

Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,15 @@ for (let i = 0; i < 256; i++) {
assert.equal(hexb2[i], hexb[i]);
}

// Test single hex character throws TypeError
// - https://github.com/nodejs/node/issues/6770
assert.throws(function() {
Buffer.from('A', 'hex');
}, TypeError);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you put a test to verify there's no regression to 'base64' since it follows the same logic path in this case? Something simple like assert.equal(Buffer.from('A', 'base64'), 0).

Copy link
Contributor Author

@jspri jspri May 17, 2016

Choose a reason for hiding this comment

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

Sure! Is there a reason why hex throws an error for odd lengths but base64 doesn't?

Edit: Don't worry, the final comment for #6107 goes into a bit.


// Test single base64 char encodes as 0
assert.strictEqual(Buffer.from('A', 'base64').length, 0);

{
// test an invalid slice end.
console.log('Try to slice off the end of the buffer');
Expand Down