From a1550a21c3da1428ce88d745181d2e0d46dcc47f Mon Sep 17 00:00:00 2001 From: mido Date: Wed, 21 Jul 2021 07:41:23 -0700 Subject: [PATCH 1/3] catch out of bounds slice/array --- compiler.go | 11 ++++++++++- variables_test.go | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/compiler.go b/compiler.go index e71ac58..4fafe3f 100644 --- a/compiler.go +++ b/compiler.go @@ -355,7 +355,16 @@ 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("index out of range [%d] with length %d", index, rv.Len()) + + } } } else { diff --git a/variables_test.go b/variables_test.go index 6f9cce9..73ea9c2 100644 --- a/variables_test.go +++ b/variables_test.go @@ -153,3 +153,13 @@ func Test_Render_Let_ArrayAssign_OutofBoundsIndex(t *testing.T) { 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) + r.Error(err) + +} From 15f4fba302f2864ade92fef5d3d9964ad5811639 Mon Sep 17 00:00:00 2001 From: mido Date: Wed, 21 Jul 2021 07:52:13 -0700 Subject: [PATCH 2/3] format the error message --- compiler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler.go b/compiler.go index 4fafe3f..092f00f 100644 --- a/compiler.go +++ b/compiler.go @@ -362,7 +362,7 @@ func (c *compiler) evalAccessIndex(left, index interface{}, node *ast.IndexExpre } else { - err = fmt.Errorf("index out of range [%d] with length %d", index, rv.Len()) + err = fmt.Errorf("array index out of bounds, got index %d, while array size is %d", index, rv.Len()) } } From 4148c2c8ebd95fc1f38a5666c0f49eb52b744b43 Mon Sep 17 00:00:00 2001 From: mido Date: Wed, 27 Oct 2021 11:15:11 -0700 Subject: [PATCH 3/3] code cleanup --- compiler.go | 3 --- variables_test.go | 7 ------- 2 files changed, 10 deletions(-) diff --git a/compiler.go b/compiler.go index 092f00f..17f570b 100644 --- a/compiler.go +++ b/compiler.go @@ -355,11 +355,8 @@ func (c *compiler) evalAccessIndex(left, index interface{}, node *ast.IndexExpre 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()) diff --git a/variables_test.go b/variables_test.go index 73ea9c2..bda526b 100644 --- a/variables_test.go +++ b/variables_test.go @@ -121,8 +121,6 @@ func Test_Render_Let_ArrayAssign_InvalidKey(t *testing.T) { input := `

<% let a = [1, 2, "three", "four", 3.75] %><% a["b"] = 3 %><%= a["c"] %>

` _, err := Render(input, NewContext()) r.Error(err) - //r.NoError(err) - //r.Equal("

", s) } func Test_Render_Let_ArrayAssign_ValidIndex(t *testing.T) { @@ -130,7 +128,6 @@ func Test_Render_Let_ArrayAssign_ValidIndex(t *testing.T) { input := `

<% let a = [1, 2, "three", "four", 3.75] %><% a[0] = 3 %><%= a[0] %>

` s, err := Render(input, NewContext()) - //r.Error(err) r.NoError(err) r.Equal("

3

", s) } @@ -140,7 +137,6 @@ func Test_Render_Let_ArrayAssign_Resultaddition(t *testing.T) { input := `

<% let a = [1, 2, "three", "four", 3.75] %><% a[4] = 3 %><%= a[4] + 2 %>

` s, err := Render(input, NewContext()) - //r.Error(err) r.NoError(err) r.Equal("

5

", s) } @@ -151,7 +147,6 @@ func Test_Render_Let_ArrayAssign_OutofBoundsIndex(t *testing.T) { input := `

<% let a = [1, 2, "three", "four", 3.75] %><% a[5] = 3 %><%= a[4] + 2 %>

` _, err := Render(input, NewContext()) r.Error(err) - } func Test_Render_Access_Array_OutofBoundsIndex(t *testing.T) { @@ -159,7 +154,5 @@ func Test_Render_Access_Array_OutofBoundsIndex(t *testing.T) { input := `<% let a = [1, 2, "three", "four", 3.75] %><%= a[5] %>` _, err := Render(input, NewContext()) - //r.Error(err) r.Error(err) - }