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

commit 17efa3969c7ea8aa14bd8d433f223eb8b294c7b7
Author: Sebastian Rühl <[email protected]>
AuthorDate: Tue Mar 28 14:18:59 2023 +0200

    test(plc4go): fix peek panic on to small buffer for test transport
---
 plc4go/spi/transports/test/Transport.go | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/plc4go/spi/transports/test/Transport.go 
b/plc4go/spi/transports/test/Transport.go
index 51a2b952ed..44a5ecc55e 100644
--- a/plc4go/spi/transports/test/Transport.go
+++ b/plc4go/spi/transports/test/Transport.go
@@ -26,6 +26,7 @@ import (
        "github.com/apache/plc4x/plc4go/spi/transports"
        "github.com/pkg/errors"
        "github.com/rs/zerolog/log"
+       "math"
        "net/url"
 )
 
@@ -111,7 +112,15 @@ func (m *TransportInstance) FillBuffer(until func(pos 
uint, currentByte byte, re
 
 func (m *TransportInstance) PeekReadableBytes(numBytes uint32) ([]uint8, 
error) {
        log.Trace().Msgf("Peek %d readable bytes", numBytes)
-       return m.readBuffer[0:numBytes], nil
+       availableBytes := uint32(math.Min(float64(numBytes), 
float64(len(m.readBuffer))))
+       var err error
+       if availableBytes != numBytes {
+               err = errors.New("not enough bytes available")
+       }
+       if availableBytes == 0 {
+               return nil, err
+       }
+       return m.readBuffer[0:availableBytes], nil
 }
 
 func (m *TransportInstance) Read(numBytes uint32) ([]uint8, error) {

Reply via email to