Skip to content

Commit

Permalink
Check index in Remove() and other functions
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Jun 23, 2021
1 parent 007d7f7 commit ef6b9dc
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion array.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func (a *ArrayDataSlab) Set(storage SlabStorage, index uint64, v Storable) error
}

func (a *ArrayDataSlab) Insert(storage SlabStorage, index uint64, v Storable) error {
if index > uint64(len(a.elements)) {
return fmt.Errorf("out of bounds")
}
if index == uint64(len(a.elements)) {
a.elements = append(a.elements, v)
} else {
Expand All @@ -119,6 +122,9 @@ func (a *ArrayDataSlab) Insert(storage SlabStorage, index uint64, v Storable) er
}

func (a *ArrayDataSlab) Remove(storage SlabStorage, index uint64) (Storable, error) {
if index >= uint64(len(a.elements)) {
return nil, fmt.Errorf("out of bounds")
}
v := a.elements[index]

switch index {
Expand Down Expand Up @@ -507,7 +513,7 @@ func (a *ArrayMetaDataSlab) Insert(storage SlabStorage, index uint64, v Storable

func (a *ArrayMetaDataSlab) Remove(storage SlabStorage, index uint64) (Storable, error) {

if index > uint64(a.header.count) {
if index >= uint64(a.header.count) {
return nil, fmt.Errorf("remove at index %d out of bounds", index)
}

Expand Down

0 comments on commit ef6b9dc

Please sign in to comment.