Skip to content

Commit

Permalink
fix: shouldn't expose index after delete
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Dec 28, 2023
1 parent ec2ab23 commit 0af3e8d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
15 changes: 15 additions & 0 deletions ast/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ func (self *linkedNodes) Len() int {
return self.size
}

func (self *linkedNodes) Get(i int) (*Node) {
if self == nil {
return nil
}
if i >= 0 && i<self.size && i < _DEFAULT_NODE_CAP {
return &self.head[i]
} else if i >= _DEFAULT_NODE_CAP && i<self.size {
a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP
if a < len(self.tail) {
return &self.tail[a][b]
}
}
return nil
}

func (self *linkedNodes) At(i int) (*Node) {
if self == nil {
return nil
Expand Down
32 changes: 5 additions & 27 deletions ast/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,9 @@ func (self *Node) encodeArray(buf *[]byte) error {

*buf = append(*buf, '[')

var s = (*linkedNodes)(self.p)
var started bool
if nb > 0 {
n := s.At(0)
if n.Exists() {
if err := n.encode(buf); err != nil {
return err
}
started = true
}
}

for i := 1; i < nb; i++ {
n := s.At(i)
for i := 0; i < nb; i++ {
n := self.nodeAt(i)
if !n.Exists() {
continue
}
Expand Down Expand Up @@ -250,21 +239,10 @@ func (self *Node) encodeObject(buf *[]byte) error {

*buf = append(*buf, '{')

var s = (*linkedPairs)(self.p)
var started bool
if nb > 0 {
n := s.At(0)
if n.Value.Exists() {
if err := n.encode(buf); err != nil {
return err
}
started = true
}
}

for i := 1; i < nb; i++ {
n := s.At(i)
if !n.Value.Exists() {
for i := 0; i < nb; i++ {
n := self.pairAt(i)
if n == nil || !n.Value.Exists() {
continue
}
if started {
Expand Down
2 changes: 1 addition & 1 deletion ast/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ next_start:
} else {
n := self.p.pairAt(self.i)
self.i++
if !n.Value.Exists() {
if n == nil || !n.Value.Exists() {
goto next_start
}
return n
Expand Down
36 changes: 31 additions & 5 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func (self *Node) UnsetByIndex(index int) (bool, error) {
it := self.itype()
if it == types.V_ARRAY {
p = self.Index(index)
}else if it == types.V_OBJECT {
} else if it == types.V_OBJECT {
if err := self.checkRaw(); err != nil {
return false, err
}
Expand Down Expand Up @@ -1174,6 +1174,19 @@ func (self *Node) nodeAt(i int) *Node {
p = &stack.v
} else {
p = (*linkedNodes)(self.p)
if l := p.Len(); l != self.len() {
// some nodes got unset, iterate to skip them
for j:=0; j<l; j++ {
v := p.At(j)
if v.Exists() {
i--
}
if i < 0 {
return v
}
}
return nil
}
}
return p.At(i)
}
Expand All @@ -1185,6 +1198,19 @@ func (self *Node) pairAt(i int) *Pair {
p = &stack.v
} else {
p = (*linkedPairs)(self.p)
if l := p.Len(); l != self.len() {
// some nodes got unset, iterate to skip them
for j:=0; j<l; j++ {
v := p.At(j)
if v != nil && v.Value.Exists() {
i--
}
if i < 0 {
return v
}
}
return nil
}
}
return p.At(i)
}
Expand Down Expand Up @@ -1336,8 +1362,8 @@ func (self *Node) removeNode(i int) {
return
}
*node = Node{}
// NOTICE: for consistency with linkedNodes, we DOSEN'T reduce size here
// self.l--
// NOTICE: not be consistent with linkedNode.Len()
self.l--
}

func (self *Node) removePair(i int) {
Expand All @@ -1346,8 +1372,8 @@ func (self *Node) removePair(i int) {
return
}
*last = Pair{}
// NOTICE: for consistency with linkedNodes, we DOSEN'T reduce size here
// self.l--
// NOTICE: should be consistent with linkedPair.Len()
self.l--
}

func (self *Node) toGenericArray() ([]interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion ast/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ func TestUnset(t *testing.T) {
hashtags := entities.Get("hashtags").Index(0)
hashtags.Set("text2", newRawNode(`{}`, types.V_OBJECT))
exist, err = hashtags.Unset("indices") // NOTICE: Unset() won't change node.Len() here
if !exist || err != nil || hashtags.len() != 3 {
if !exist || err != nil || hashtags.len() != 2 {
t.Fatal(hashtags.len())
}
y, _ := hashtags.Get("text").String()
Expand Down

0 comments on commit 0af3e8d

Please sign in to comment.