From feb0f0db3122ee10dd75083af7589b74008024d7 Mon Sep 17 00:00:00 2001 From: Elara6331 Date: Sun, 11 Feb 2024 22:11:17 -0800 Subject: [PATCH] Fix nil index handling --- salix.go | 2 +- structs_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/salix.go b/salix.go index 4892522..d41114a 100644 --- a/salix.go +++ b/salix.go @@ -415,7 +415,7 @@ func (t *Template) getIndex(i ast.Index, local map[string]any) (any, error) { return nil, ast.PosError(i, "%s: cannot get index of nil value", valueToString(i)) } rindex := reflect.ValueOf(index) - if !rval.IsValid() { + if !rindex.IsValid() { return nil, ast.PosError(i, "%s: cannot use nil value as an index", valueToString(i)) } diff --git a/structs_test.go b/structs_test.go index 588c2ee..6ce6573 100644 --- a/structs_test.go +++ b/structs_test.go @@ -107,6 +107,23 @@ func TestGetIndexNil(t *testing.T) { } } +func TestGetIndexNilIndex(t *testing.T) { + testMap := map[string]any{} + tmpl := testTmpl(t) + + // test[index] + ast := ast.Index{ + Value: ast.Ident{Value: "test", Position: testPos(t)}, + Index: ast.Ident{Value: "index", Position: testPos(t)}, + Position: testPos(t), + } + + _, err := tmpl.getIndex(ast, map[string]any{"test": testMap, "index": nil}) + if err == nil { + t.Error("Expected error, got nil") + } +} + func TestGetField(t *testing.T) { testStruct := struct { X int