Skip to content

Commit

Permalink
Merge pull request #201 from evanphx/b-null
Browse files Browse the repository at this point in the history
Validate that the partialDoc is decoded correctly
  • Loading branch information
evanphx committed Jan 27, 2024
2 parents b82b685 + 009bc56 commit 1bcbd0f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions v5/patch.go
Expand Up @@ -38,6 +38,8 @@ var (
ErrInvalid = errors.New("invalid state detected")
ErrInvalidIndex = errors.New("invalid index referenced")

ErrExpectedObject = errors.New("invalid value, expected object")

rawJSONArray = []byte("[]")
rawJSONObject = []byte("{}")
rawJSONNull = []byte("null")
Expand Down Expand Up @@ -134,6 +136,10 @@ func (n *lazyNode) UnmarshalJSON(data []byte) error {
}

func (n *partialDoc) TrustMarshalJSON(buf *bytes.Buffer) error {
if n.obj == nil {
return ErrExpectedObject
}

if err := buf.WriteByte('{'); err != nil {
return err
}
Expand Down Expand Up @@ -557,6 +563,10 @@ func findObject(pd *container, path string, options *ApplyOptions) (container, s
}

func (d *partialDoc) set(key string, val *lazyNode, options *ApplyOptions) error {
if d.obj == nil {
return ErrExpectedObject
}

found := false
for _, k := range d.keys {
if k == key {
Expand All @@ -579,6 +589,11 @@ func (d *partialDoc) get(key string, options *ApplyOptions) (*lazyNode, error) {
if key == "" {
return d.self, nil
}

if d.obj == nil {
return nil, ErrExpectedObject
}

v, ok := d.obj[key]
if !ok {
return v, errors.Wrapf(ErrMissing, "unable to get nonexistent key: %s", key)
Expand All @@ -587,6 +602,10 @@ func (d *partialDoc) get(key string, options *ApplyOptions) (*lazyNode, error) {
}

func (d *partialDoc) remove(key string, options *ApplyOptions) error {
if d.obj == nil {
return ErrExpectedObject
}

_, ok := d.obj[key]
if !ok {
if options.AllowMissingPathOnRemove {
Expand Down

0 comments on commit 1bcbd0f

Please sign in to comment.