From e9d485b7f60fe854db407ae3bbd767a5eb30feec Mon Sep 17 00:00:00 2001 From: James Shubin Date: Fri, 9 May 2025 01:08:12 -0400 Subject: [PATCH] lang: ast, core: Add some safety checks I don't think I'm hitting these, but good for debugging. --- lang/ast/structs.go | 22 ++++++++++++++++++++++ lang/core/collect.go | 3 +++ 2 files changed, 25 insertions(+) diff --git a/lang/ast/structs.go b/lang/ast/structs.go index 38b80ac5..f3c2daa8 100644 --- a/lang/ast/structs.go +++ b/lang/ast/structs.go @@ -868,9 +868,18 @@ func (obj *StmtRes) Output(table map[interfaces.Func]types.Value) (*interfaces.O } s := name.Str() // must not panic + if s == "" { + return nil, fmt.Errorf("empty name") + } names = append(names, s) // host is the input telling us who we want to pull from hosts[s] = host.Str() // correspondence map + if hosts[s] == "" { + return nil, fmt.Errorf("empty host") + } + if hosts[s] == "*" { // safety + return nil, fmt.Errorf("invalid star host") + } } for n, m := range mapping { // delete everything else if !util.StrInList(n, names) { @@ -999,6 +1008,19 @@ func (obj *StmtRes) collect(table map[interfaces.Func]types.Value) (map[string]m n := name.Str() // must not panic h := host.Str() // must not panic + if n == "" { + // programming error + return nil, fmt.Errorf("name field is empty") + } + if h == "" { + // programming error + return nil, fmt.Errorf("host field is empty") + } + if h == "*" { + // programming error + return nil, fmt.Errorf("host field is a start") + } + if _, exists := m[n]; !exists { m[n] = make(map[string]string) } diff --git a/lang/core/collect.go b/lang/core/collect.go index 692661da..ac679cb7 100644 --- a/lang/core/collect.go +++ b/lang/core/collect.go @@ -482,6 +482,9 @@ func (obj *CollectFunc) Call(ctx context.Context, args []types.Value) (types.Val if x.Host == "" { return nil, fmt.Errorf("unexpected empty host") } + if x.Host == "*" { // safety check + return nil, fmt.Errorf("unexpected star host") + } if x.Data == "" { return nil, fmt.Errorf("unexpected empty data") }