From e32a4b12ecc0653598a34cbe44f82a99352aedcf Mon Sep 17 00:00:00 2001 From: Jarrah Date: Thu, 19 Oct 2023 13:24:36 -0400 Subject: [PATCH] Fix unchecked type assertion in Subscription Stats Fixes the following panic: github.com/gopcua/opcua.(*Subscription).StatsWithContext(0xc0000a48c0, {0xf66c28, 0x1545c20}) github.com/gopcua/opcua@v0.3.11/subscription.go:315 +0x207 github.com/gopcua/opcua.(*Subscription).Stats(...) github.com/gopcua/opcua@v0.3.11/subscription.go:296 --- subscription.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/subscription.go b/subscription.go index 7318f2ba..f08a81f6 100644 --- a/subscription.go +++ b/subscription.go @@ -276,8 +276,16 @@ func (s *Subscription) Stats(ctx context.Context) (*ua.SubscriptionDiagnosticsDa return nil, errors.Errorf("empty SubscriptionDiagnostics for sub=%d", s.SubscriptionID) } - for _, eo := range v.Value().([]*ua.ExtensionObject) { - stat := eo.Value.(*ua.SubscriptionDiagnosticsDataType) + eos, ok := v.Value().([]*ua.ExtensionObject) + if !ok { + return nil, errors.Errorf("invalid type for SubscriptionDiagnosticsArray. Want []*ua.ExtensionObject. subID=%d nodeID=%s type=%T", s.SubscriptionID, node.String(), v.Value()) + } + + for _, eo := range eos { + stat, ok := eo.Value.(*ua.SubscriptionDiagnosticsDataType) + if !ok { + continue + } if stat.SubscriptionID == s.SubscriptionID { return stat, nil