Skip to content

Value.Patch: "add" with an out-of-range array index appends instead of failing #52

Description

@RaphaelFakhri

What happened

An add operation whose array index is greater than the array length succeeds and appends the value.

v, _ := hujson.Parse([]byte(`[1,2]`))
err := v.Patch([]byte(`[{"op":"add","path":"/5","value":3}]`))
fmt.Println(err, string(v.Pack()))
// <nil> [1,2,3]

What you expected

RFC 6902, section 4.1 states that the index of an add on an array must not be greater than the number of elements. The patch should fail with an error and leave the array unchanged. Indexes equal to the length and the - token continue to append.

Cause

patchAdd in patch.go accepts any errNotFound from find as an insertion point. For arrays, find reports the array length as the index for every missing index, so /5 is treated the same as /2 or /-.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions