Skip to content

Commit

Permalink
fix: let starved fixed-length arrays throw on encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
cha0s committed Nov 26, 2024
1 parent 2b4ce4e commit 5354ef1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/codecs/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
12 changes: 12 additions & 0 deletions src/codecs/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 5354ef1

Please sign in to comment.