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

[plc4x] 01/01: fix(plc4go/modbus): Delete elements in the loop, and the index is decremented by 1 at the same time

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

hongjinlin pushed a commit to branch fix/PLC4GO-BATCH-WRITE-ERROR
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit aadd33cedc79ed09d24bbce0e9148c129e189a21
Author: Jinlin Hong <89...@qq.com>
AuthorDate: Tue Jul 11 17:52:38 2023 +0800

    fix(plc4go/modbus): Delete elements in the loop, and the index is decremented by 1 at the same time
---
 plc4go/spi/default/DefaultCodec.go | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/plc4go/spi/default/DefaultCodec.go b/plc4go/spi/default/DefaultCodec.go
index 875302e2d1..2232765b63 100644
--- a/plc4go/spi/default/DefaultCodec.go
+++ b/plc4go/spi/default/DefaultCodec.go
@@ -251,7 +251,8 @@ func (m *defaultCodec) HandleMessages(message spi.Message) bool {
 	defer m.expectationsChangeMutex.Unlock()
 	messageHandled := false
 	m.log.Trace().Msgf("Current number of expectations: %d", len(m.expectations))
-	for index, expectation := range m.expectations {
+	for i := 0; i < len(m.expectations); i++ {
+		expectation := m.expectations[i]
 		m.log.Trace().Msgf("Checking expectation %s", expectation)
 		// Check if the current message matches the expectations
 		// If it does, let it handle the message.
@@ -269,10 +270,11 @@ func (m *defaultCodec) HandleMessages(message spi.Message) bool {
 			}
 			messageHandled = true
 			// If this is the last element of the list remove it differently than if it's before that
-			if (index + 1) == len(m.expectations) {
-				m.expectations = m.expectations[:index]
-			} else if (index + 1) < len(m.expectations) {
-				m.expectations = append(m.expectations[:index], m.expectations[index+1:]...)
+			if (i + 1) == len(m.expectations) {
+				m.expectations = m.expectations[:i]
+			} else if (i + 1) < len(m.expectations) {
+				m.expectations = append(m.expectations[:i], m.expectations[i+1:]...)
+				i--
 			}
 		} else {
 			m.log.Trace().Stringer("expectation", expectation).Msg("doesn't accept message")