Skip to content

Commit 0f31dab

Browse files
authored
Merge pull request #23 from antoinelyset/guard-out-of-bound-array-insert
Raise an error when inserting in out of bounds array index
2 parents 9eecb10 + 4eee3cb commit 0f31dab

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

lib/json1.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ function removeChild(container: Doc[] | DocObj, key: Key) {
157157
function insertChildMut(container: Doc[] | DocObj, key: Key, value: Doc) {
158158
if (typeof key === 'number') {
159159
assert(Array.isArray(container), 'Cannot use numerical key for object container')
160+
assert(container.length >= key, 'Cannot insert into out of bounds index')
160161
//container = container.slice()
161162
container.splice(key, 0, value)
162163
} else {

test/test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,10 @@ describe('json1', () => {
636636

637637
it('throws if the type is missing', () =>
638638
assert.throws(() => type.apply({}, [{ et: 'missing', e: {} }])))
639+
640+
it('throws when the op inserts in out of bound index', () => {
641+
assert.throws(() => type.apply([0], [2, { i: 2 }]))
642+
})
639643
})
640644

641645
describe('apply path', () => {

0 commit comments

Comments
 (0)