From 1bb1e056c4dfb277631e5cd996df5f96c37eb580 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Mon, 17 Mar 2025 02:28:40 -0400 Subject: [PATCH] lang: funcs: structs: Add some extra safety checks Not sure if these are even needed. --- lang/funcs/structs/const.go | 10 ++++++++++ lang/funcs/structs/if.go | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/lang/funcs/structs/const.go b/lang/funcs/structs/const.go index 38965b89..c9696a3b 100644 --- a/lang/funcs/structs/const.go +++ b/lang/funcs/structs/const.go @@ -111,6 +111,16 @@ func (obj *ConstFunc) Stream(ctx context.Context) error { // Call this function with the input args and return the value if it is possible // to do so at this time. func (obj *ConstFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) { + if obj.Info() == nil { + return nil, fmt.Errorf("info is empty") + } + if obj.Info().Sig == nil { + return nil, fmt.Errorf("sig is empty") + } + if i, j := len(args), len(obj.Info().Sig.Ord); i != j { + return nil, fmt.Errorf("arg length doesn't match, got %d, exp: %d", i, j) + } + if obj.Value == nil { return nil, fmt.Errorf("no value available from const") } diff --git a/lang/funcs/structs/if.go b/lang/funcs/structs/if.go index 253c3647..a242ea67 100644 --- a/lang/funcs/structs/if.go +++ b/lang/funcs/structs/if.go @@ -148,6 +148,16 @@ func (obj *IfFunc) Stream(ctx context.Context) error { // to do so at this time. // XXX: Is is correct to implement this here for this particular function? func (obj *IfFunc) Call(ctx context.Context, args []types.Value) (types.Value, error) { + if obj.Info() == nil { + return nil, fmt.Errorf("info is empty") + } + if obj.Info().Sig == nil { + return nil, fmt.Errorf("sig is empty") + } + if i, j := len(args), len(obj.Info().Sig.Ord); i != j { + return nil, fmt.Errorf("arg length doesn't match, got %d, exp: %d", i, j) + } + if c := args[0].Bool(); c { return args[1], nil // true branch }