Skip to content

Commit 009bc56

Browse files
committed
Validate that the partialDoc is decoded correctly
1 parent b82b685 commit 009bc56

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

v5/patch.go

+19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ var (
3838
ErrInvalid = errors.New("invalid state detected")
3939
ErrInvalidIndex = errors.New("invalid index referenced")
4040

41+
ErrExpectedObject = errors.New("invalid value, expected object")
42+
4143
rawJSONArray = []byte("[]")
4244
rawJSONObject = []byte("{}")
4345
rawJSONNull = []byte("null")
@@ -134,6 +136,10 @@ func (n *lazyNode) UnmarshalJSON(data []byte) error {
134136
}
135137

136138
func (n *partialDoc) TrustMarshalJSON(buf *bytes.Buffer) error {
139+
if n.obj == nil {
140+
return ErrExpectedObject
141+
}
142+
137143
if err := buf.WriteByte('{'); err != nil {
138144
return err
139145
}
@@ -557,6 +563,10 @@ func findObject(pd *container, path string, options *ApplyOptions) (container, s
557563
}
558564

559565
func (d *partialDoc) set(key string, val *lazyNode, options *ApplyOptions) error {
566+
if d.obj == nil {
567+
return ErrExpectedObject
568+
}
569+
560570
found := false
561571
for _, k := range d.keys {
562572
if k == key {
@@ -579,6 +589,11 @@ func (d *partialDoc) get(key string, options *ApplyOptions) (*lazyNode, error) {
579589
if key == "" {
580590
return d.self, nil
581591
}
592+
593+
if d.obj == nil {
594+
return nil, ErrExpectedObject
595+
}
596+
582597
v, ok := d.obj[key]
583598
if !ok {
584599
return v, errors.Wrapf(ErrMissing, "unable to get nonexistent key: %s", key)
@@ -587,6 +602,10 @@ func (d *partialDoc) get(key string, options *ApplyOptions) (*lazyNode, error) {
587602
}
588603

589604
func (d *partialDoc) remove(key string, options *ApplyOptions) error {
605+
if d.obj == nil {
606+
return ErrExpectedObject
607+
}
608+
590609
_, ok := d.obj[key]
591610
if !ok {
592611
if options.AllowMissingPathOnRemove {

0 commit comments

Comments
 (0)