lang: ast, core: Add some safety checks

I don't think I'm hitting these, but good for debugging.
This commit is contained in:
James Shubin
2025-05-09 01:08:12 -04:00
parent e86d66b906
commit e9d485b7f6
2 changed files with 25 additions and 0 deletions

View File

@@ -868,9 +868,18 @@ func (obj *StmtRes) Output(table map[interfaces.Func]types.Value) (*interfaces.O
} }
s := name.Str() // must not panic s := name.Str() // must not panic
if s == "" {
return nil, fmt.Errorf("empty name")
}
names = append(names, s) names = append(names, s)
// host is the input telling us who we want to pull from // host is the input telling us who we want to pull from
hosts[s] = host.Str() // correspondence map 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 for n, m := range mapping { // delete everything else
if !util.StrInList(n, names) { 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 n := name.Str() // must not panic
h := host.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 { if _, exists := m[n]; !exists {
m[n] = make(map[string]string) m[n] = make(map[string]string)
} }

View File

@@ -482,6 +482,9 @@ func (obj *CollectFunc) Call(ctx context.Context, args []types.Value) (types.Val
if x.Host == "" { if x.Host == "" {
return nil, fmt.Errorf("unexpected empty host") return nil, fmt.Errorf("unexpected empty host")
} }
if x.Host == "*" { // safety check
return nil, fmt.Errorf("unexpected star host")
}
if x.Data == "" { if x.Data == "" {
return nil, fmt.Errorf("unexpected empty data") return nil, fmt.Errorf("unexpected empty data")
} }