From 3dcaf12ac6967e6525acb9ab9963ec86279d9fb1 Mon Sep 17 00:00:00 2001 From: Zach Zhu Date: Thu, 8 Jul 2021 19:31:36 +0800 Subject: [PATCH 1/2] Add gitignore --- .gitignore | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7ed7f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# editor and IDE paraphernalia +.idea +.vscode + +# macOS paraphernalia +.DS_Store From 19e83eee5ca03c508de93a54b45162e838458c67 Mon Sep 17 00:00:00 2001 From: Zach Zhu Date: Thu, 8 Jul 2021 19:33:00 +0800 Subject: [PATCH 2/2] Fix partial negative indice support --- patch.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/patch.go b/patch.go index 1829854..4bce593 100644 --- a/patch.go +++ b/patch.go @@ -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 } @@ -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) }