You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2023/05/11 09:23:21 UTC

[plc4x] branch develop updated: fix(plc4go/knx): avoid panics

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 93289741b4 fix(plc4go/knx): avoid panics
93289741b4 is described below

commit 93289741b42d325d0d4f2f7a07ba163947911e8e
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Thu May 11 11:23:07 2023 +0200

    fix(plc4go/knx): avoid panics
---
 plc4go/internal/knxnetip/Reader.go            | 15 ++++++++++++---
 plc4go/internal/knxnetip/SubscriptionEvent.go | 10 +++++++++-
 plc4go/internal/knxnetip/Utils.go             | 26 +++++++++++++-------------
 3 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/plc4go/internal/knxnetip/Reader.go b/plc4go/internal/knxnetip/Reader.go
index c27e42f8a1..24b5548102 100644
--- a/plc4go/internal/knxnetip/Reader.go
+++ b/plc4go/internal/knxnetip/Reader.go
@@ -22,6 +22,7 @@ package knxnetip
 import (
 	"context"
 	"errors"
+	"github.com/rs/zerolog/log"
 	"strconv"
 	"strings"
 	"time"
@@ -168,6 +169,7 @@ func (m Reader) Read(ctx context.Context, readRequest apiModel.PlcReadRequest) <
 func (m Reader) readGroupAddress(ctx context.Context, tag GroupAddressTag) (apiModel.PlcResponseCode, apiValues.PlcValue) {
 	rawAddresses, err := m.resolveAddresses(tag)
 	if err != nil {
+		log.Debug().Err(err).Msg("error resolving addresses")
 		return apiModel.PlcResponseCode_INVALID_ADDRESS, nil
 	}
 
@@ -178,8 +180,11 @@ func (m Reader) readGroupAddress(ctx context.Context, tag GroupAddressTag) (apiM
 	returnCodes := map[string]apiModel.PlcResponseCode{}
 	for _, numericAddress := range rawAddresses {
 		// Create a string representation of this numeric address depending on the type of requested address
-		stringAddress := NumericGroupAddressToString(numericAddress, tag)
-
+		stringAddress, err := NumericGroupAddressToString(numericAddress, tag)
+		if err != nil {
+			log.Debug().Err(err).Msg("error mapping addresses")
+			return apiModel.PlcResponseCode_INVALID_ADDRESS, nil
+		}
 		// Try to get a value from the cache
 		m.connection.valueCacheMutex.RLock()
 		int8s, ok := m.connection.valueCache[numericAddress]
@@ -233,7 +238,11 @@ func (m Reader) readGroupAddress(ctx context.Context, tag GroupAddressTag) (apiM
 	// If there is only one address to read, return this directly.
 	// Otherwise, return a struct, with the keys being the string representations of the address.
 	if len(rawAddresses) == 1 {
-		stringAddress := NumericGroupAddressToString(rawAddresses[0], tag)
+		stringAddress, err := NumericGroupAddressToString(rawAddresses[0], tag)
+		if err != nil {
+			log.Debug().Err(err).Msg("error mapping addresses")
+			return apiModel.PlcResponseCode_INVALID_ADDRESS, nil
+		}
 		return apiModel.PlcResponseCode_OK, values[stringAddress]
 	} else if len(rawAddresses) > 1 {
 		// Add it to the result
diff --git a/plc4go/internal/knxnetip/SubscriptionEvent.go b/plc4go/internal/knxnetip/SubscriptionEvent.go
index 2f90fdd275..c02a45b3e5 100644
--- a/plc4go/internal/knxnetip/SubscriptionEvent.go
+++ b/plc4go/internal/knxnetip/SubscriptionEvent.go
@@ -26,6 +26,8 @@ import (
 	"github.com/apache/plc4x/plc4go/pkg/api/values"
 	driverModel "github.com/apache/plc4x/plc4go/protocols/knxnetip/readwrite/model"
 	internalModel "github.com/apache/plc4x/plc4go/spi/model"
+
+	"github.com/rs/zerolog/log"
 )
 
 type SubscriptionEvent struct {
@@ -57,7 +59,13 @@ func (m SubscriptionEvent) GetAddress(name string) string {
 		groupAddress, err = driverModel.KnxGroupAddressParse(rawAddress, 1)
 	}
 	if err != nil {
+		log.Debug().Err(err).Msg("error parsing")
+		return ""
+	}
+	toString, err := GroupAddressToString(groupAddress)
+	if err != nil {
+		log.Debug().Err(err).Msg("error mapping")
 		return ""
 	}
-	return GroupAddressToString(groupAddress)
+	return toString
 }
diff --git a/plc4go/internal/knxnetip/Utils.go b/plc4go/internal/knxnetip/Utils.go
index 7b72aa5635..decf05e3e0 100644
--- a/plc4go/internal/knxnetip/Utils.go
+++ b/plc4go/internal/knxnetip/Utils.go
@@ -20,49 +20,49 @@
 package knxnetip
 
 import (
-	"fmt"
+	"github.com/pkg/errors"
 	"strconv"
 
 	driverModel "github.com/apache/plc4x/plc4go/protocols/knxnetip/readwrite/model"
 )
 
-func NumericGroupAddressToString(numericAddress uint16, groupAddress GroupAddressTag) string {
+func NumericGroupAddressToString(numericAddress uint16, groupAddress GroupAddressTag) (string, error) {
 	if groupAddress == nil {
-		return ""
+		return "", nil
 	}
 	switch groupAddress.(type) {
 	case GroupAddress3LevelPlcTag:
 		main := numericAddress >> 11
 		middle := (numericAddress >> 8) & 0x07
 		sub := numericAddress & 0xFF
-		return strconv.Itoa(int(main)) + "/" + strconv.Itoa(int(middle)) + "/" + strconv.Itoa(int(sub))
+		return strconv.Itoa(int(main)) + "/" + strconv.Itoa(int(middle)) + "/" + strconv.Itoa(int(sub)), nil
 	case GroupAddress2LevelPlcTag:
 		main := numericAddress >> 11
 		sub := numericAddress & 0x07FF
-		return strconv.Itoa(int(main)) + "/" + strconv.Itoa(int(sub))
+		return strconv.Itoa(int(main)) + "/" + strconv.Itoa(int(sub)), nil
 	case GroupAddress1LevelPlcTag:
-		return strconv.Itoa(int(numericAddress))
+		return strconv.Itoa(int(numericAddress)), nil
 	default:
-		panic(fmt.Sprintf("Unmapped %T", groupAddress))
+		return "", errors.Errorf("Unmapped %T", groupAddress)
 	}
 }
 
-func GroupAddressToString(groupAddress driverModel.KnxGroupAddress) string {
+func GroupAddressToString(groupAddress driverModel.KnxGroupAddress) (string, error) {
 	if groupAddress == nil {
-		return ""
+		return "", nil
 	}
 	switch groupAddress := groupAddress.(type) {
 	case driverModel.KnxGroupAddress3Level:
 		level3 := groupAddress
-		return strconv.Itoa(int(level3.GetMainGroup())) + "/" + strconv.Itoa(int(level3.GetMiddleGroup())) + "/" + strconv.Itoa(int(level3.GetSubGroup()))
+		return strconv.Itoa(int(level3.GetMainGroup())) + "/" + strconv.Itoa(int(level3.GetMiddleGroup())) + "/" + strconv.Itoa(int(level3.GetSubGroup())), nil
 	case driverModel.KnxGroupAddress2Level:
 		level2 := groupAddress
-		return strconv.Itoa(int(level2.GetMainGroup())) + "/" + strconv.Itoa(int(level2.GetSubGroup()))
+		return strconv.Itoa(int(level2.GetMainGroup())) + "/" + strconv.Itoa(int(level2.GetSubGroup())), nil
 	case driverModel.KnxGroupAddressFreeLevel:
 		level1 := groupAddress
-		return strconv.Itoa(int(level1.GetSubGroup()))
+		return strconv.Itoa(int(level1.GetSubGroup())), nil
 	default:
-		panic(fmt.Sprintf("Unmapped %T", groupAddress))
+		return "", errors.Errorf("Unmapped %T", groupAddress)
 	}
 }