Skip to content

Commit

Permalink
buffer: fix single digit hex string handling
Browse files Browse the repository at this point in the history
Fixes single digit hex strings not raising TypeError on Buffer creation.

Fixes: #6770
PR-URL: #6775
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
jspri authored and Fishrock123 committed May 23, 2016
1 parent 136c098 commit feb037b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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);

// 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

0 comments on commit feb037b

Please sign in to comment.