From 3cc745891ef2e89e933bea893933f5351f02ef8a Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Sun, 21 Jan 2018 00:01:16 +0100 Subject: [PATCH] Throw error on alloc() if value can't be encoded --- index.js | 4 ++++ test/node/test-buffer-alloc.js | 8 ++++---- test/node/test-buffer-fill.js | 22 ++++++++++++++++++---- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index b30bb96..042a787 100644 --- a/index.js +++ b/index.js @@ -1552,6 +1552,10 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) { ? val : new Buffer(val, encoding) var len = bytes.length + if (len === 0) { + throw new TypeError('The value "' + val + + '" is invalid for argument "value"') + } for (i = 0; i < end - start; ++i) { this[i + start] = bytes[i % len] } diff --git a/test/node/test-buffer-alloc.js b/test/node/test-buffer-alloc.js index 32a9763..d7d4ee4 100644 --- a/test/node/test-buffer-alloc.js +++ b/test/node/test-buffer-alloc.js @@ -1011,25 +1011,25 @@ assert.strictEqual(Buffer.prototype.toLocaleString, Buffer.prototype.toString); var buf = Buffer.from('test'); assert.strictEqual(buf.toLocaleString(), buf.toString()); } -/* + common.expectsError(() => { Buffer.alloc(0x1000, 'This is not correctly encoded', 'hex'); }, { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); - +/* common.expectsError(() => { Buffer.alloc(0x1000, 'c', 'hex'); }, { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); - +*/ common.expectsError(() => { Buffer.alloc(1, Buffer.alloc(0)); }, { code: 'ERR_INVALID_ARG_VALUE', type: TypeError }); -*/ + diff --git a/test/node/test-buffer-fill.js b/test/node/test-buffer-fill.js index 7138094..eb88b83 100644 --- a/test/node/test-buffer-fill.js +++ b/test/node/test-buffer-fill.js @@ -1,6 +1,6 @@ 'use strict'; var Buffer = require('../../').Buffer; - +const common = require('../common'); var assert = require('assert'); @@ -137,10 +137,24 @@ testBufs('c8a26161', 8, 1, 'hex'); testBufs('61c8b462c8b563c8b6', 4, -1, 'hex'); testBufs('61c8b462c8b563c8b6', 4, 1, 'hex'); testBufs('61c8b462c8b563c8b6', 12, 1, 'hex'); -// Make sure this operation doesn't go on forever -buf1.fill('yKJh', 'hex'); -assert.throws(() => buf1.fill('\u0222', 'hex')); +common.expectsError(() => { + const buf = Buffer.allocUnsafe(SIZE); + + buf.fill('yKJh', 'hex'); +}, { + code: 'ERR_INVALID_ARG_VALUE', + type: TypeError +}); + +common.expectsError(() => { + const buf = Buffer.allocUnsafe(SIZE); + + buf.fill('\u0222', 'hex'); +}, { + code: 'ERR_INVALID_ARG_VALUE', + type: TypeError +}); // BASE64 testBufs('YWJj', 'ucs2'); -- 2.34.1