Skip to content

Commit

Permalink
Avoid negative indexes on array-accesses.
Browse files Browse the repository at this point in the history
Found via fuzzing :)
  • Loading branch information
skx committed Dec 22, 2018
1 parent 114562c commit 8ec555e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,17 @@ func Array(x int, y int) *ArrayObject {

// Get the value at the given X,Y coordinate
func (a *ArrayObject) Get(x int, y int) Object {
offset := x*a.X + y
offset := int(x*a.X + y)

if a.X == 0 && offset >= a.Y {
return &ErrorObject{Value: "Get-Array access out of bounds (Y)"}
}
if (a.X != 0) && (offset > a.X*a.Y) {
return &ErrorObject{Value: "Get-Array access out of bounds (X,Y)"}
}
if offset < 0 {
return &ErrorObject{Value: "Get-Array access out of bounds (negative index)"}
}
if offset > len(a.Contents) {
return &ErrorObject{Value: "Get-Array access out of bounds (LEN)"}
}
Expand All @@ -104,14 +107,17 @@ func (a *ArrayObject) Get(x int, y int) Object {

// Set the value at the given X,Y coordinate
func (a *ArrayObject) Set(x int, y int, obj Object) Object {
offset := x*a.X + y
offset := int(x*a.X + y)

if a.X == 0 && offset >= a.Y {
return &ErrorObject{Value: "Set-Array access out of bounds (Y)"}
}
if (a.X != 0) && (offset > a.X*a.Y) {
return &ErrorObject{Value: "Set-Array access out of bounds (X,Y)"}
}
if offset < 0 {
return &ErrorObject{Value: "Set-Array access out of bounds (negative index)"}
}
if offset > len(a.Contents) {
return &ErrorObject{Value: "Set-Array access out of bounds (LEN)"}
}
Expand Down
13 changes: 13 additions & 0 deletions object/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ func Test1DArray(t *testing.T) {
if err2.Type() != ERROR {
t.Errorf("Expected a bounds-error, got none")
}

// Test that we have negative bound-checking on Get
err3 := a.Get(0, -6)
if err3.Type() != ERROR {
t.Errorf("Expected a bounds-error, got none")
}

// Test that we have negative bound-checking on Set
err4 := a.Set(0, -6, err1)
if err4.Type() != ERROR {
t.Errorf("Expected a bounds-error, got none")
}

}

func Test2DArray(t *testing.T) {
Expand Down

0 comments on commit 8ec555e

Please sign in to comment.