Skip to content

Commit

Permalink
Merge pull request #156 from Mido-sys/fix_panic_outofbounds_struct
Browse files Browse the repository at this point in the history
fix outofbounds access to a nested struct
  • Loading branch information
paganotoni authored Jun 8, 2022
2 parents e68597e + 897776b commit 2e14ef3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
13 changes: 7 additions & 6 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,18 @@ func (c *compiler) evalAccessIndex(left, index interface{}, node *ast.IndexExpre

case reflect.Array, reflect.Slice:
if i, ok := index.(int); ok {
if i < 0 || i >= rv.Len() {
err = fmt.Errorf("array index out of bounds, got index %d, while array size is %d", index, rv.Len())

if node.Callee != nil {
} else {

returnValue, err = c.evalIndexCallee(rv.Index(i), node)
if node.Callee != nil {

returnValue, err = c.evalIndexCallee(rv.Index(i), node)

} else {
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())
returnValue = rv.Index(i).Interface()

}
}
Expand Down
19 changes: 19 additions & 0 deletions for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,22 @@ func Test_Render_For_Map_Nil_Value(t *testing.T) {
r.NoError(err)
r.Equal("", strings.TrimSpace(s))
}

type Category struct {
Products []Product
}
type Product struct {
Name []string
}

func Test_Render_For_Array_OutofBoundIndex(t *testing.T) {
r := require.New(t)
ctx := NewContext()
product_listing := Category{}
ctx.Set("product_listing", product_listing)
input := `<%= for (i, names) in product_listing.Products[0].Name { %>
<%= splt %>
<% } %>`
_, err := Render(input, ctx)
r.Error(err)
}
17 changes: 17 additions & 0 deletions variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,20 @@ func Test_Render_Access_Array_OutofBoundsIndex(t *testing.T) {
_, err := Render(input, NewContext())
r.Error(err)
}

type Category1 struct {
Products []Product1
}
type Product1 struct {
Name []string
}

func Test_Render_Access_CalleeArray_OutofBoundIndex(t *testing.T) {
r := require.New(t)
ctx := NewContext()
product_listing := Category1{}
ctx.Set("product_listing", product_listing)
input := `<% let a = product_listing.Products[0].Name[0] %><%= a %>`
_, err := Render(input, ctx)
r.Error(err)
}

0 comments on commit 2e14ef3

Please sign in to comment.