From ea384230edc92a566330cf8108f9582ac7131257 Mon Sep 17 00:00:00 2001 From: William Poussier Date: Mon, 11 Nov 2024 18:22:58 +0100 Subject: [PATCH] fix: bound check on patch insert Closes #26 --- differ.go | 4 ++-- hash.go | 3 +-- operation.go | 7 +++++-- operation_test.go | 5 +++++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/differ.go b/differ.go index 540fe7f..fe6111d 100644 --- a/differ.go +++ b/differ.go @@ -339,7 +339,7 @@ func (d *Differ) compareArraysLCS(ptr pointer, src, tgt []interface{}, doc strin adjust := func(i int) int { // Adjust indice considering add and remove - // operations that precede. + // operations that precede it. return i + add - remove } @@ -483,7 +483,7 @@ func (d *Differ) add(path string, v interface{}, doc string, lcs bool) { if !lcs { d.patch = d.patch.append(OperationMove, op.Path, path, v, v, 0) } else { - d.patch = d.patch.prepend(d.snapshotPatchLen, OperationMove, op.Path, path, v, v, 0) + d.patch = d.patch.insert(d.snapshotPatchLen, OperationMove, op.Path, path, v, v, 0) } } return diff --git a/hash.go b/hash.go index 86ded75..4c52434 100644 --- a/hash.go +++ b/hash.go @@ -73,8 +73,7 @@ func (h *hasher) sortArray(a []interface{}) { return 1 } else if d1 < d2 { return -1 - } else { - return 0 } + return 0 }) } diff --git a/operation.go b/operation.go index 4859f6a..6c08835 100644 --- a/operation.go +++ b/operation.go @@ -118,7 +118,10 @@ func (p *Patch) append(typ string, from, path string, src, tgt interface{}, vl i }) } -func (p *Patch) prepend(startIdx int, typ string, from, path string, src, tgt interface{}, vl int) Patch { +func (p *Patch) insert(pos int, typ string, from, path string, src, tgt interface{}, vl int) Patch { + if pos > len(*p) { + return p.append(typ, from, path, src, tgt, vl) + } op := Operation{ Type: typ, From: from, @@ -127,7 +130,7 @@ func (p *Patch) prepend(startIdx int, typ string, from, path string, src, tgt in Value: tgt, valueLen: vl, } - return append((*p)[:startIdx], append([]Operation{op}, (*p)[startIdx:]...)...) + return append((*p)[:pos], append([]Operation{op}, (*p)[pos:]...)...) } func (p *Patch) jsonLength() int { diff --git a/operation_test.go b/operation_test.go index e6fbe80..85550f6 100644 --- a/operation_test.go +++ b/operation_test.go @@ -152,6 +152,11 @@ func TestPatch_jsonLength(t *testing.T) { }) } +func Test_issue26(_ *testing.T) { + p := new(Patch) + p.insert(6, OperationAdd, emptyPointer, "/a", nil, 42, 0) +} + func typeNilIface() interface{} { var i *int var p interface{}