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/21 12:13:55 UTC

(plc4x) branch develop updated: fix(plc4j/codegen): count curPos in bytes, as the other generators do

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 90accc0633 fix(plc4j/codegen): count curPos in bytes, as the other generators do
90accc0633 is described below

commit 90accc0633ab4beb5a6717ef0461024c34a3ba34
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Aug 21 14:13:20 2026 +0200

    fix(plc4j/codegen): count curPos in bytes, as the other generators do
    
    curPos is compared against lengths read off the wire, which are byte counts, and
    the Go and C generators both resolve it to a byte position. The Java generator
    resolved it to a bit position, so every such comparison was out by a factor of
    eight - and being a subtraction it went negative, which a length array reads as
    empty rather than as an error.
    
    S7 was parsing no COTP parameters at all: for a connection request the length
    came out as 18 - 56 instead of 18 - 7, so the negotiated TPDU size and TSAPs
    were silently dropped. Nothing caught it because plc4j had no testsuite wired up
    for s7, and plc4go was already correct.
    
    The `curPos / 8` in the cotp and ads mspecs was compensating for this in the one
    language that needed it, and was wrong in the two that did not - plc4go's ads
    readers were dividing a byte count by eight. With curPos consistent the
    compensation goes, leaving every use of it bare.
    
    Takes the s7 parser-serializer testsuite from 9 failures to 3.
---
 .../codegeneration/language/java/generators/BaseGenerator.java   | 9 +++++++--
 plc4go/protocols/ads/readwrite/model/AdsDataTypeTableEntry.go    | 2 +-
 plc4go/protocols/ads/readwrite/model/AdsMethodInfo.go            | 2 +-
 plc4go/protocols/ads/readwrite/model/AdsSymbolTableEntry.go      | 2 +-
 .../apache/plc4x/java/ads/readwrite/AdsDataTypeTableEntry.java   | 2 +-
 .../org/apache/plc4x/java/ads/readwrite/AdsMethodInfo.java       | 2 +-
 .../org/apache/plc4x/java/ads/readwrite/AdsSymbolTableEntry.java | 2 +-
 .../generated/org/apache/plc4x/java/s7/readwrite/COTPPacket.java | 4 ++--
 .../org/apache/plc4x/java/cotp/readwrite/COTPPacket.java         | 4 ++--
 protocols/ads/src/main/resources/protocols/ads/ads.mspec         | 6 +++---
 protocols/cotp/src/main/resources/protocols/cotp/cotp.mspec      | 4 ++--
 11 files changed, 22 insertions(+), 17 deletions(-)

diff --git a/code-generation/language/java/src/main/java/org/apache/plc4x/codegeneration/language/java/generators/BaseGenerator.java b/code-generation/language/java/src/main/java/org/apache/plc4x/codegeneration/language/java/generators/BaseGenerator.java
index 64b47f009b..d441240d5c 100644
--- a/code-generation/language/java/src/main/java/org/apache/plc4x/codegeneration/language/java/generators/BaseGenerator.java
+++ b/code-generation/language/java/src/main/java/org/apache/plc4x/codegeneration/language/java/generators/BaseGenerator.java
@@ -661,10 +661,15 @@ public abstract class BaseGenerator<T> {
             }
             case VariableLiteral variableLiteral -> {
                 if ("curPos".equals(variableLiteral.getName())) {
+                    // curPos counts bytes, as it does in the Go and C generators, and as every
+                    // mspec using it assumes: it is compared against byte lengths taken off the
+                    // wire. Emitting the bit position instead made those comparisons wrong by a
+                    // factor of eight, which for a subtraction went negative and silently produced
+                    // an empty array.
                     if (isParse) {
-                        return CodeBlock.of("(readBuffer.getPositionInBits() - startPos)");
+                        return CodeBlock.of("((readBuffer.getPositionInBits() - startPos) / 8)");
                     } else {
-                        return CodeBlock.of("(writeBuffer.getPositionInBits() - startPos)");
+                        return CodeBlock.of("((writeBuffer.getPositionInBits() - startPos) / 8)");
                     }
                 }
                 // If this literal references an Enum type, then we have to output it differently.
diff --git a/plc4go/protocols/ads/readwrite/model/AdsDataTypeTableEntry.go b/plc4go/protocols/ads/readwrite/model/AdsDataTypeTableEntry.go
index 2850968d69..5ca29727b6 100644
--- a/plc4go/protocols/ads/readwrite/model/AdsDataTypeTableEntry.go
+++ b/plc4go/protocols/ads/readwrite/model/AdsDataTypeTableEntry.go
@@ -1384,7 +1384,7 @@ func (m *_AdsDataTypeTableEntry) parse(ctx context.Context, readBuffer utils.Rea
 		m.ExtendedInfos = extendedInfos
 	}
 
-	rest, err := readBuffer.ReadByteArray("rest", int(int32(entryLength)-int32((int32((positionAware.GetPos()-startPos))/int32(int32(8))))), codegen.WithEncoding("UTF8"), codegen.WithByteOrder(binary.LittleEndian))
+	rest, err := readBuffer.ReadByteArray("rest", int(int32(entryLength)-int32((positionAware.GetPos()-startPos))), codegen.WithEncoding("UTF8"), codegen.WithByteOrder(binary.LittleEndian))
 	if err != nil {
 		return nil, errors.Wrap(err, fmt.Sprintf("Error parsing 'rest' field"))
 	}
diff --git a/plc4go/protocols/ads/readwrite/model/AdsMethodInfo.go b/plc4go/protocols/ads/readwrite/model/AdsMethodInfo.go
index a3dc196e9a..fb0cb73277 100644
--- a/plc4go/protocols/ads/readwrite/model/AdsMethodInfo.go
+++ b/plc4go/protocols/ads/readwrite/model/AdsMethodInfo.go
@@ -594,7 +594,7 @@ func (m *_AdsMethodInfo) parse(ctx context.Context, readBuffer utils.ReadBuffer)
 	}
 	m.Parameters = parameters
 
-	rest, err := readBuffer.ReadByteArray("rest", int(int32(methodInfoLength)-int32((int32((positionAware.GetPos()-startPos))/int32(int32(8))))))
+	rest, err := readBuffer.ReadByteArray("rest", int(int32(methodInfoLength)-int32((positionAware.GetPos()-startPos))))
 	if err != nil {
 		return nil, errors.Wrap(err, fmt.Sprintf("Error parsing 'rest' field"))
 	}
diff --git a/plc4go/protocols/ads/readwrite/model/AdsSymbolTableEntry.go b/plc4go/protocols/ads/readwrite/model/AdsSymbolTableEntry.go
index 163e0cffc6..d58ea67f65 100644
--- a/plc4go/protocols/ads/readwrite/model/AdsSymbolTableEntry.go
+++ b/plc4go/protocols/ads/readwrite/model/AdsSymbolTableEntry.go
@@ -1021,7 +1021,7 @@ func (m *_AdsSymbolTableEntry) parse(ctx context.Context, readBuffer utils.ReadB
 		m.Attributes = attributes
 	}
 
-	rest, err := readBuffer.ReadByteArray("rest", int(int32(entryLength)-int32((int32((positionAware.GetPos()-startPos))/int32(int32(8))))), codegen.WithEncoding("UTF8"), codegen.WithByteOrder(binary.LittleEndian))
+	rest, err := readBuffer.ReadByteArray("rest", int(int32(entryLength)-int32((positionAware.GetPos()-startPos))), codegen.WithEncoding("UTF8"), codegen.WithByteOrder(binary.LittleEndian))
 	if err != nil {
 		return nil, errors.Wrap(err, fmt.Sprintf("Error parsing 'rest' field"))
 	}
diff --git a/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsDataTypeTableEntry.java b/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsDataTypeTableEntry.java
index 85a4573f55..0cab3340c4 100644
--- a/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsDataTypeTableEntry.java
+++ b/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsDataTypeTableEntry.java
@@ -673,7 +673,7 @@ public class AdsDataTypeTableEntry implements Message {
     AdsExtendedInfos extendedInfos = FieldReaderFactory.readOptionalField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(AdsExtendedInfos.class, AdsExtendedInfos.staticParse(readBuffer, (org.apache.plc4x.java.ads.readwrite.AdsDatatypeId) (dataType))), readBuffer), flagExtendedInfos, WithOption.WithName("extendedInfos"), WithOption.WithSignedIntegerEncoding("twos-complement"), WithOption.WithUnsignedIntegerEncoding("unsigned-binary"), WithByteBasedOption.WithByte [...]
 
     // Array Field: rest
-    byte[] rest = readBuffer.readBits(Math.toIntExact(((entryLength) - ((((readBuffer.getPositionInBits() - startPos)) / (8)))) * 8), WithOption.WithName("rest"), WithOption.WithSignedIntegerEncoding("twos-complement"), WithOption.WithUnsignedIntegerEncoding("unsigned-binary"), WithByteBasedOption.WithByteOrder("LITTLE_ENDIAN"), WithOption.WithStringEncoding("UTF8"));
+    byte[] rest = readBuffer.readBits(Math.toIntExact(((entryLength) - (((readBuffer.getPositionInBits() - startPos) / 8))) * 8), WithOption.WithName("rest"), WithOption.WithSignedIntegerEncoding("twos-complement"), WithOption.WithUnsignedIntegerEncoding("unsigned-binary"), WithByteBasedOption.WithByteOrder("LITTLE_ENDIAN"), WithOption.WithStringEncoding("UTF8"));
 
     readBuffer.popContext();
     return new AdsDataTypeTableEntry(entryLength, version, hashValue, typeHashValue, size, offset, dataType, flagPropItem, flagBitValues, flagOversample, flagMethodRef, flagReferenceTo, flagTComInterfacePtr, flagCopyMask, flagPersistent, flagPlcPointerType, flagInitOnReset, flagPersistentDataType, flagAnySizeArray, flagIgnorePersist, flagSoftwareProtectionLevels, flagStatic, flagAligned, ExtendedFlags, flagExtendedEnumInfos, flagDeRefTypeItem, flagContainsOnlineChangePtrRef, flagIncomple [...]
diff --git a/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsMethodInfo.java b/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsMethodInfo.java
index 25d8f9e88f..7b1506bd05 100644
--- a/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsMethodInfo.java
+++ b/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsMethodInfo.java
@@ -260,7 +260,7 @@ public class AdsMethodInfo implements Message {
     List<AdsMethodParam> parameters = FieldReaderFactory.readCountArrayField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(AdsMethodParam.class, AdsMethodParam.staticParse(readBuffer)), readBuffer), parameterCount, WithOption.WithName("parameters"));
 
     // Array Field: rest
-    byte[] rest = readBuffer.readBits(Math.toIntExact(((methodInfoLength) - ((((readBuffer.getPositionInBits() - startPos)) / (8)))) * 8), WithOption.WithName("rest"));
+    byte[] rest = readBuffer.readBits(Math.toIntExact(((methodInfoLength) - (((readBuffer.getPositionInBits() - startPos) / 8))) * 8), WithOption.WithName("rest"));
 
     readBuffer.popContext();
     return new AdsMethodInfo(header1, header2, header3, header4, header5, guid, methodId, methodFlags, name, typeName, comment, parameters, rest);
diff --git a/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsSymbolTableEntry.java b/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsSymbolTableEntry.java
index e220a4520c..880af53f82 100644
--- a/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsSymbolTableEntry.java
+++ b/plc4j/drivers/ads/src/main/generated/org/apache/plc4x/java/ads/readwrite/AdsSymbolTableEntry.java
@@ -486,7 +486,7 @@ public class AdsSymbolTableEntry implements Message {
     AdsDataTypeAttributes attributes = FieldReaderFactory.readOptionalField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(AdsDataTypeAttributes.class, AdsDataTypeAttributes.staticParse(readBuffer)), readBuffer), flagAttributes, WithOption.WithName("attributes"), WithOption.WithSignedIntegerEncoding("twos-complement"), WithOption.WithUnsignedIntegerEncoding("unsigned-binary"), WithByteBasedOption.WithByteOrder("LITTLE_ENDIAN"), WithOption.WithStringEncoding("UTF8"));
 
     // Array Field: rest
-    byte[] rest = readBuffer.readBits(Math.toIntExact(((entryLength) - ((((readBuffer.getPositionInBits() - startPos)) / (8)))) * 8), WithOption.WithName("rest"), WithOption.WithSignedIntegerEncoding("twos-complement"), WithOption.WithUnsignedIntegerEncoding("unsigned-binary"), WithByteBasedOption.WithByteOrder("LITTLE_ENDIAN"), WithOption.WithStringEncoding("UTF8"));
+    byte[] rest = readBuffer.readBits(Math.toIntExact(((entryLength) - (((readBuffer.getPositionInBits() - startPos) / 8))) * 8), WithOption.WithName("rest"), WithOption.WithSignedIntegerEncoding("twos-complement"), WithOption.WithUnsignedIntegerEncoding("unsigned-binary"), WithByteBasedOption.WithByteOrder("LITTLE_ENDIAN"), WithOption.WithStringEncoding("UTF8"));
 
     readBuffer.popContext();
     return new AdsSymbolTableEntry(entryLength, group, offset, size, dataType, flagMethodDeref, flagItfMethodAccess, flagReadOnly, flagTComInterfacePointer, flagReferenceTo, flagBitValue, flagPersistent, flagCompilerGenerated, flagSystemServiceSymbol, flagExtendedFlags, flagInitOnReset, flagStatic, flagVariantType, flagOnlineChangePtrRefType, flagRefactorInfo, flagRedundancyIgnore, flagPlcPointerType, name, dataTypeName, comment, contextMask, guid, attributes, rest);
diff --git a/plc4j/drivers/s7/src/main/generated/org/apache/plc4x/java/s7/readwrite/COTPPacket.java b/plc4j/drivers/s7/src/main/generated/org/apache/plc4x/java/s7/readwrite/COTPPacket.java
index a10dc4708b..fffcb44573 100644
--- a/plc4j/drivers/s7/src/main/generated/org/apache/plc4x/java/s7/readwrite/COTPPacket.java
+++ b/plc4j/drivers/s7/src/main/generated/org/apache/plc4x/java/s7/readwrite/COTPPacket.java
@@ -93,10 +93,10 @@ public abstract class COTPPacket implements Message {
     }
 
     // Array Field: parameters
-    List<COTPParameter> parameters = FieldReaderFactory.readLengthArrayField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(COTPParameter.class, COTPParameter.staticParse(readBuffer, (short) ((((headerLength) + (1))) - ((readBuffer.getPositionInBits() - startPos))))), readBuffer), (((headerLength) + (1))) - ((readBuffer.getPositionInBits() - startPos)), WithOption.WithName("parameters"));
+    List<COTPParameter> parameters = FieldReaderFactory.readLengthArrayField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(COTPParameter.class, COTPParameter.staticParse(readBuffer, (short) ((((headerLength) + (1))) - (((readBuffer.getPositionInBits() - startPos) / 8))))), readBuffer), (((headerLength) + (1))) - (((readBuffer.getPositionInBits() - startPos) / 8)), WithOption.WithName("parameters"));
 
     // Optional Field (conditional): payload
-    S7Message payload = FieldReaderFactory.readOptionalField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(S7Message.class, S7Message.staticParse(readBuffer)), readBuffer), ((readBuffer.getPositionInBits() - startPos)) < (cotpLen), WithOption.WithName("payload"));
+    S7Message payload = FieldReaderFactory.readOptionalField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(S7Message.class, S7Message.staticParse(readBuffer)), readBuffer), (((readBuffer.getPositionInBits() - startPos) / 8)) < (cotpLen), WithOption.WithName("payload"));
 
     readBuffer.popContext();
     return builder.build(parameters, payload);
diff --git a/plc4j/transports/cotp/src/main/generated/org/apache/plc4x/java/cotp/readwrite/COTPPacket.java b/plc4j/transports/cotp/src/main/generated/org/apache/plc4x/java/cotp/readwrite/COTPPacket.java
index a9adbc9f00..32a79640dc 100644
--- a/plc4j/transports/cotp/src/main/generated/org/apache/plc4x/java/cotp/readwrite/COTPPacket.java
+++ b/plc4j/transports/cotp/src/main/generated/org/apache/plc4x/java/cotp/readwrite/COTPPacket.java
@@ -94,10 +94,10 @@ public abstract class COTPPacket implements Message {
     }
 
     // Array Field: parameters
-    List<COTPParameter> parameters = FieldReaderFactory.readLengthArrayField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(COTPParameter.class, COTPParameter.staticParse(readBuffer, (short) ((((headerLength) + (1))) - ((((readBuffer.getPositionInBits() - startPos)) / (8)))))), readBuffer), (((headerLength) + (1))) - ((((readBuffer.getPositionInBits() - startPos)) / (8))), WithOption.WithName("parameters"));
+    List<COTPParameter> parameters = FieldReaderFactory.readLengthArrayField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(COTPParameter.class, COTPParameter.staticParse(readBuffer, (short) ((((headerLength) + (1))) - (((readBuffer.getPositionInBits() - startPos) / 8))))), readBuffer), (((headerLength) + (1))) - (((readBuffer.getPositionInBits() - startPos) / 8)), WithOption.WithName("parameters"));
 
     // Array Field: payload
-    byte[] payload = readBuffer.readBits(Math.toIntExact(((cotpLen) - ((((readBuffer.getPositionInBits() - startPos)) / (8)))) * 8), WithOption.WithName("payload"));
+    byte[] payload = readBuffer.readBits(Math.toIntExact(((cotpLen) - (((readBuffer.getPositionInBits() - startPos) / 8))) * 8), WithOption.WithName("payload"));
 
     readBuffer.popContext();
     return builder.build(parameters, payload);
diff --git a/protocols/ads/src/main/resources/protocols/ads/ads.mspec b/protocols/ads/src/main/resources/protocols/ads/ads.mspec
index 062882703f..2b49b426f5 100644
--- a/protocols/ads/src/main/resources/protocols/ads/ads.mspec
+++ b/protocols/ads/src/main/resources/protocols/ads/ads.mspec
@@ -931,7 +931,7 @@
     [array    byte                             guid                       count         'flagTypeGuid == true ? 16 : 0']
     [optional AdsDataTypeAttributes            attributes                 'flagAttributes'                             ]
     // Gobbling up the rest, but it seems there is only empty padding bytes in it.
-	[array    byte                             rest                       count            'entryLength - (curPos / 8)']
+	[array    byte                             rest                       count            'entryLength - curPos']
 ]
 
 // https://gitlab.com/xilix-systems-llc/go-native-ads/-/blob/master/symbols.go#L15
@@ -1004,7 +1004,7 @@
     [optional AdsDataTypeAttributes              attributes               'flagAttributes'                             ]
     [optional AdsExtendedInfos('dataType')       extendedInfos            'flagExtendedInfos'                          ]
 	// This only consumes the rest in cased of both methodInfos and extendedInfos not being set.
-	[array    byte                               rest                     count            'entryLength - (curPos / 8)']
+	[array    byte                               rest                     count            'entryLength - curPos']
 ]
 
 [type AdsDataTypeArrayInfo byteOrder='"LITTLE_ENDIAN"'
@@ -1043,7 +1043,7 @@
 	[const    uint 8                       commentTerminator  0x00                                                     ]
     [array    AdsMethodParam               parameters         count         'parameterCount'                           ]
 	// This only consumes the rest in cases, where we haven't quite figgured out the full structure of this type.
-	[array    byte                         rest               count         'methodInfoLength - (curPos / 8)'          ]
+	[array    byte                         rest               count         'methodInfoLength - curPos'          ]
 ]
 
 // A single parameter descriptor. Matches your “tail”: length + 5 headers + GUID +
diff --git a/protocols/cotp/src/main/resources/protocols/cotp/cotp.mspec b/protocols/cotp/src/main/resources/protocols/cotp/cotp.mspec
index dcaa855a38..5f34daf133 100644
--- a/protocols/cotp/src/main/resources/protocols/cotp/cotp.mspec
+++ b/protocols/cotp/src/main/resources/protocols/cotp/cotp.mspec
@@ -64,8 +64,8 @@
             [simple uint 8            rejectCause                                                                      ]
         ]
     ]
-    [array    COTPParameter ('(headerLength + 1) - (curPos / 8)') parameters length '(headerLength + 1) - (curPos / 8)']
-    [array    byte                                                payload    count 'cotpLen - (curPos / 8)'            ]
+    [array    COTPParameter ('(headerLength + 1) - curPos') parameters length '(headerLength + 1) - curPos']
+    [array    byte                                                payload    count 'cotpLen - curPos'            ]
 ]
 
 [discriminatedType COTPParameter (uint 8 restSizeInBytes)