Skip to content

Commit

Permalink
Merge pull request #145 from Mido-sys/fix_panic_outbounds_slice
Browse files Browse the repository at this point in the history
catch out of bounds slice/array index access
  • Loading branch information
paganotoni authored Oct 27, 2021
2 parents c2f85ef + 4148c2c commit 140b5c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 7 additions & 1 deletion compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,13 @@ func (c *compiler) evalAccessIndex(left, index interface{}, node *ast.IndexExpre
returnValue, err = c.evalIndexCallee(rv.Index(i), node)

} else {
returnValue = rv.Index(i).Interface()
if i >= 0 && i < rv.Len() {
returnValue = rv.Index(i).Interface()
} else {

err = fmt.Errorf("array index out of bounds, got index %d, while array size is %d", index, rv.Len())

}
}

} else {
Expand Down
11 changes: 7 additions & 4 deletions variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,13 @@ func Test_Render_Let_ArrayAssign_InvalidKey(t *testing.T) {
input := `<p><% let a = [1, 2, "three", "four", 3.75] %><% a["b"] = 3 %><%= a["c"] %></p>`
_, err := Render(input, NewContext())
r.Error(err)
//r.NoError(err)
//r.Equal("<p></p>", s)
}

func Test_Render_Let_ArrayAssign_ValidIndex(t *testing.T) {
r := require.New(t)

input := `<p><% let a = [1, 2, "three", "four", 3.75] %><% a[0] = 3 %><%= a[0] %></p>`
s, err := Render(input, NewContext())
//r.Error(err)
r.NoError(err)
r.Equal("<p>3</p>", s)
}
Expand All @@ -140,7 +137,6 @@ func Test_Render_Let_ArrayAssign_Resultaddition(t *testing.T) {

input := `<p><% let a = [1, 2, "three", "four", 3.75] %><% a[4] = 3 %><%= a[4] + 2 %></p>`
s, err := Render(input, NewContext())
//r.Error(err)
r.NoError(err)
r.Equal("<p>5</p>", s)
}
Expand All @@ -151,5 +147,12 @@ func Test_Render_Let_ArrayAssign_OutofBoundsIndex(t *testing.T) {
input := `<p><% let a = [1, 2, "three", "four", 3.75] %><% a[5] = 3 %><%= a[4] + 2 %></p>`
_, err := Render(input, NewContext())
r.Error(err)
}

func Test_Render_Access_Array_OutofBoundsIndex(t *testing.T) {
r := require.New(t)

input := `<% let a = [1, 2, "three", "four", 3.75] %><%= a[5] %>`
_, err := Render(input, NewContext())
r.Error(err)
}

0 comments on commit 140b5c2

Please sign in to comment.