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: make byteLength throw on invalid input #8946

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
2 changes: 0 additions & 2 deletions doc/api/buffer.md
Expand Up @@ -631,8 +631,6 @@ console.log(`${str}: ${str.length} characters, ` +
When `string` is a `Buffer`/[`DataView`]/[`TypedArray`]/[`ArrayBuffer`], the
actual byte length is returned.

Otherwise, converts to `String` and returns the byte length of string.

### Class Method: Buffer.compare(buf1, buf2)
<!-- YAML
added: v0.11.13
Expand Down
2 changes: 1 addition & 1 deletion lib/buffer.js
Expand Up @@ -369,7 +369,7 @@ function byteLength(string, encoding) {
return string.byteLength;
}

string = '' + string;
throw new TypeError('"string" must be a string, Buffer, or ArrayBuffer');
}

var len = string.length;
Expand Down
12 changes: 8 additions & 4 deletions test/parallel/test-buffer-bytelength.js
Expand Up @@ -7,10 +7,14 @@ const SlowBuffer = require('buffer').SlowBuffer;
const vm = require('vm');

// coerce values to string
assert.strictEqual(Buffer.byteLength(32, 'latin1'), 2);
assert.strictEqual(Buffer.byteLength(NaN, 'utf8'), 3);
assert.strictEqual(Buffer.byteLength({}, 'latin1'), 15);
assert.strictEqual(Buffer.byteLength(), 9);
assert.throws(() => { Buffer.byteLength(32, 'latin1'); },
'"string" must be a string, Buffer, or ArrayBuffer');
assert.throws(() => { Buffer.byteLength(NaN, 'utf8'); },
'"string" must be a string, Buffer, or ArrayBuffer');
assert.throws(() => { Buffer.byteLength({}, 'latin1'); },
'"string" must be a string, Buffer, or ArrayBuffer');
assert.throws(() => { Buffer.byteLength(); },
'"string" must be a string, Buffer, or ArrayBuffer');

assert(ArrayBuffer.isView(new Buffer(10)));
assert(ArrayBuffer.isView(new SlowBuffer(10)));
Expand Down