buffer: make byteLength throw on invalid input
PR-URL: https://github.com/nodejs/node/pull/8946 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com>
This commit is contained in:
parent
01db04bd30
commit
c9dade48b4
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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)));
|
||||
|
Loading…
x
Reference in New Issue
Block a user