Skip to content

Commit

Permalink
Merge pull request #146 from zqzten/fix_partial_negative_indice_support
Browse files Browse the repository at this point in the history
Fix partial negative indice support in v4
  • Loading branch information
evanphx committed Oct 20, 2021
2 parents 556cb64 + 19e83ee commit df14171
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# editor and IDE paraphernalia
.idea
.vscode

# macOS paraphernalia
.DS_Store
21 changes: 21 additions & 0 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ func (d *partialArray) set(key string, val *lazyNode) error {
if err != nil {
return err
}

if idx < 0 {
if !SupportNegativeIndices {
return errors.Wrapf(ErrInvalidIndex, "Unable to access invalid index: %d", idx)
}
if idx < -len(*d) {
return errors.Wrapf(ErrInvalidIndex, "Unable to access invalid index: %d", idx)
}
idx += len(*d)
}

(*d)[idx] = val
return nil
}
Expand Down Expand Up @@ -462,6 +473,16 @@ func (d *partialArray) get(key string) (*lazyNode, error) {
return nil, err
}

if idx < 0 {
if !SupportNegativeIndices {
return nil, errors.Wrapf(ErrInvalidIndex, "Unable to access invalid index: %d", idx)
}
if idx < -len(*d) {
return nil, errors.Wrapf(ErrInvalidIndex, "Unable to access invalid index: %d", idx)
}
idx += len(*d)
}

if idx >= len(*d) {
return nil, errors.Wrapf(ErrInvalidIndex, "Unable to access invalid index: %d", idx)
}
Expand Down

0 comments on commit df14171

Please sign in to comment.