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 2026/08/18 20:22:27 UTC

(plc4x) 09/17: fix(plc4go/eip): compute multi-service offsets in int to keep the underflow guard live

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 3ef33bfdda464f35dd75c108a77e3687c4728c27
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Tue Aug 18 17:32:10 2026 +0200

    fix(plc4go/eip): compute multi-service offsets in int to keep the underflow guard live
---
 plc4go/internal/eip/Reader.go      |  4 +--
 plc4go/internal/eip/Reader_test.go | 69 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/plc4go/internal/eip/Reader.go b/plc4go/internal/eip/Reader.go
index a89997f345..f777e227b6 100644
--- a/plc4go/internal/eip/Reader.go
+++ b/plc4go/internal/eip/Reader.go
@@ -292,12 +292,12 @@ func sliceService(servicesData []byte, offsets []uint16, i, nb int) (readWriteMo
 	if len(offsets) < nb {
 		return nil, false
 	}
-	offset := int(offsets[i] - offsets[0])
+	offset := int(offsets[i]) - int(offsets[0])
 	var length int
 	if i == nb-1 {
 		length = len(servicesData) - offset
 	} else {
-		length = int(offsets[i+1]-offsets[0]) - offset
+		length = int(offsets[i+1]) - int(offsets[0]) - offset
 	}
 	if offset < 0 || length <= 0 || offset+length > len(servicesData) {
 		return nil, false
diff --git a/plc4go/internal/eip/Reader_test.go b/plc4go/internal/eip/Reader_test.go
new file mode 100644
index 0000000000..e4294fe378
--- /dev/null
+++ b/plc4go/internal/eip/Reader_test.go
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package eip
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+
+	readWriteModel "github.com/apache/plc4x/plc4go/protocols/eip/readwrite/model"
+)
+
+// TestSliceServiceHappyPath exercises a valid 2-entry MultipleServiceResponse
+// offset table, so the reversed-table test below isn't the only case covered.
+func TestSliceServiceHappyPath(t *testing.T) {
+	service1, err := readWriteModel.NewCipReadResponse(0, 0,
+		readWriteModel.NewCIPData(readWriteModel.CIPDataTypeCode_DINT, []byte{0x01, 0x00, 0x00, 0x00})).Serialize()
+	require.NoError(t, err)
+	service2, err := readWriteModel.NewCipReadResponse(0, 0,
+		readWriteModel.NewCIPData(readWriteModel.CIPDataTypeCode_DINT, []byte{0x02, 0x00, 0x00, 0x00})).Serialize()
+	require.NoError(t, err)
+
+	servicesData := append(append([]byte{}, service1...), service2...)
+	offsets := []uint16{0, uint16(len(service1))}
+
+	first, ok := sliceService(servicesData, offsets, 0, 2)
+	require.True(t, ok)
+	firstResponse, isReadResponse := first.(readWriteModel.CipReadResponse)
+	require.True(t, isReadResponse)
+	assert.Equal(t, []byte{0x01, 0x00, 0x00, 0x00}, firstResponse.GetData().GetData())
+
+	second, ok := sliceService(servicesData, offsets, 1, 2)
+	require.True(t, ok)
+	secondResponse, isReadResponse := second.(readWriteModel.CipReadResponse)
+	require.True(t, isReadResponse)
+	assert.Equal(t, []byte{0x02, 0x00, 0x00, 0x00}, secondResponse.GetData().GetData())
+}
+
+// TestSliceServiceReversedOffsetsIsRejected guards against a lying/reversed
+// offset table wrapping around in unsigned 16-bit arithmetic before the
+// bounds guards run. offsets[0] sits near 0xFFFF while offsets[1] sits near
+// 0, so a naive `uint16(offsets[i] - offsets[0])` subtraction would wrap to a
+// small positive number instead of tripping the "offset < 0" guard.
+func TestSliceServiceReversedOffsetsIsRejected(t *testing.T) {
+	servicesData := make([]byte, 8)
+	offsets := []uint16{0xFFF0, 2}
+
+	service, ok := sliceService(servicesData, offsets, 1, 2)
+	assert.False(t, ok)
+	assert.Nil(t, service)
+}