From 5354ef1cc82466b5328257f5caba981fe8ee2c17 Mon Sep 17 00:00:00 2001 From: cha0s Date: Tue, 26 Nov 2024 15:30:29 -0600 Subject: [PATCH] fix: let starved fixed-length arrays throw on encoding --- src/codecs/array.js | 4 ++-- src/codecs/array.test.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/codecs/array.js b/src/codecs/array.js index ed9cee0..36145c4 100644 --- a/src/codecs/array.js +++ b/src/codecs/array.js @@ -121,7 +121,7 @@ class ArrayCodec { } let protocol = value[Symbol.iterator](); let result = protocol.next(); - for (let i = 0; !result.done && i < ${length}; ++i) { + for (let i = 0; i < ${length}; ++i) { written += this.$$elementCodec.encode(result.value, view, byteOffset + written); result = protocol.next(); } @@ -144,7 +144,7 @@ class ArrayCodec { } let protocol = value[Symbol.iterator](); let result = protocol.next(); - for (let i = 0; !result.done && i < length; ++i) { + for (let i = 0; i < length; ++i) { size += this.$$elementCodec.size(result.value); result = protocol.next(); } diff --git a/src/codecs/array.test.js b/src/codecs/array.test.js index 7f46f2a..b43d316 100644 --- a/src/codecs/array.test.js +++ b/src/codecs/array.test.js @@ -109,3 +109,15 @@ test('fixed-length string drop', async () => { expect(codec.encode(value, view, 0)).to.equal(size); expect(Array.from(codec.decode(view, {byteOffset: 0}))).to.deep.equal(value.slice(0, length)); }); + +test('fixed-length string starved', async () => { + const length = 3; + const codec = new Codec({ + element: {type: 'string'}, + length, + }); + const value = ['one', 'two']; + expect(() => codec.size(value)).toThrowError(); + const view = new DataView(new ArrayBuffer(1024)); + expect(() => codec.encode(value, view, 0)).toThrowError(); +});