Skip to content

Commit a69e7e0

Browse files
authored
Merge pull request #276 from dbarrosop/master
fix: prevent panic on negative slice index in Delete with malformed JSON (GO-2026-4514)
2 parents 61b32cf + d3eacc0 commit a69e7e0

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

parser.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ func Delete(data []byte, keys ...string) []byte {
731731
if !array {
732732
if len(keys) > 1 {
733733
_, _, startOffset, endOffset, err = internalGet(data, keys[:lk-1]...)
734-
if err == KeyPathNotFoundError {
734+
if err != nil {
735735
// problem parsing the data
736736
return data
737737
}
@@ -743,7 +743,11 @@ func Delete(data []byte, keys ...string) []byte {
743743
return data
744744
}
745745
keyOffset += startOffset
746-
_, _, _, subEndOffset, _ := internalGet(data[startOffset:endOffset], keys[lk-1])
746+
var subEndOffset int
747+
_, _, _, subEndOffset, err = internalGet(data[startOffset:endOffset], keys[lk-1])
748+
if err != nil {
749+
return data
750+
}
747751
endOffset = startOffset + subEndOffset
748752
tokEnd := tokenEnd(data[endOffset:])
749753
tokStart := findTokenStart(data[:keyOffset], ","[0])
@@ -757,7 +761,7 @@ func Delete(data []byte, keys ...string) []byte {
757761
}
758762
} else {
759763
_, _, keyOffset, endOffset, err = internalGet(data, keys...)
760-
if err == KeyPathNotFoundError {
764+
if err != nil {
761765
// problem parsing the data
762766
return data
763767
}

parser_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,24 @@ var deleteTests = []DeleteTest{
227227
path: []string{"test"},
228228
data: ` {}`,
229229
},
230+
{
231+
desc: "GO-2026-4514: malformed JSON without enclosing braces should not panic",
232+
json: `"0":"0":`,
233+
path: []string{"0"},
234+
data: `"0":"0":`,
235+
},
236+
{
237+
desc: "GO-2026-4514: malformed JSON with key but truncated value should not panic",
238+
json: `{"a": `,
239+
path: []string{"a"},
240+
data: `{"a": `,
241+
},
242+
{
243+
desc: "GO-2026-4514: malformed nested JSON with truncated value should not panic",
244+
json: `{"a":{"b": `,
245+
path: []string{"a", "b"},
246+
data: `{"a":{"b": `,
247+
},
230248
}
231249

232250
var setTests = []SetTest{

0 commit comments

Comments
 (0)