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 08:51:20 UTC
(plc4x) 01/02: fix(bacnetip): require an announced optional field to actually be present
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 1f96a034886879d29ed6d400bb4d506f6ce181f8
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Aug 21 10:49:02 2026 +0200
fix(bacnetip): require an announced optional field to actually be present
A field read under a condition can still end up unset, because a read that runs
out of bytes is reported as an absent optional rather than as a failure. Four
types then derived a value straight from such a field, guarded only by the same
condition that read it: the integer tag payloads via actualValue, and the
segmented confirmed request and complex ack via segmentServiceChoice.
Extend the validation of each to require the field it goes on to use, so a
message cut short is rejected where it was previously carried forward with a
field that was never read. NPDU already guarded its lengths this way.
---
.../bacnetip/readwrite/model/APDUComplexAck.go | 2 +-
.../readwrite/model/APDUConfirmedRequest.go | 2 +-
.../model/BACnetTagPayloadSignedInteger.go | 4 +-
.../model/BACnetTagPayloadUnsignedInteger.go | 4 +-
.../model/BACnetTruncatedOptional_test.go | 70 ++++++++++++++++++++++
.../java/bacnetip/readwrite/APDUComplexAck.java | 2 +-
.../bacnetip/readwrite/APDUConfirmedRequest.java | 2 +-
.../readwrite/BACnetTagPayloadSignedInteger.java | 4 +-
.../readwrite/BACnetTagPayloadUnsignedInteger.java | 4 +-
.../resources/protocols/bacnetip/bacnet-tags.mspec | 8 ++-
.../resources/protocols/bacnetip/bacnetip.mspec | 6 +-
11 files changed, 92 insertions(+), 16 deletions(-)
diff --git a/plc4go/protocols/bacnetip/readwrite/model/APDUComplexAck.go b/plc4go/protocols/bacnetip/readwrite/model/APDUComplexAck.go
index 9f25bc2d53..a8c25ac303 100644
--- a/plc4go/protocols/bacnetip/readwrite/model/APDUComplexAck.go
+++ b/plc4go/protocols/bacnetip/readwrite/model/APDUComplexAck.go
@@ -489,7 +489,7 @@ func (m *_APDUComplexAck) parse(ctx context.Context, readBuffer utils.ReadBuffer
}
// Validation
- if !(bool((bool(!(segmentedMessage)) && bool(bool((serviceAck) != (nil))))) || bool(segmentedMessage)) {
+ if !(bool((bool(!(segmentedMessage)) && bool(bool((serviceAck) != (nil))))) || bool((bool(bool(segmentedMessage) && bool(bool((sequenceNumber) != (nil)))) && bool(bool((proposedWindowSize) != (nil)))))) {
return nil, errors.WithStack(utils.ParseValidationError{Message: "service ack should be set"})
}
diff --git a/plc4go/protocols/bacnetip/readwrite/model/APDUConfirmedRequest.go b/plc4go/protocols/bacnetip/readwrite/model/APDUConfirmedRequest.go
index 39415d9b5f..e572ffcfe4 100644
--- a/plc4go/protocols/bacnetip/readwrite/model/APDUConfirmedRequest.go
+++ b/plc4go/protocols/bacnetip/readwrite/model/APDUConfirmedRequest.go
@@ -561,7 +561,7 @@ func (m *_APDUConfirmedRequest) parse(ctx context.Context, readBuffer utils.Read
}
// Validation
- if !(bool((bool(!(segmentedMessage)) && bool(bool((serviceRequest) != (nil))))) || bool(segmentedMessage)) {
+ if !(bool((bool(!(segmentedMessage)) && bool(bool((serviceRequest) != (nil))))) || bool((bool(bool(segmentedMessage) && bool(bool((sequenceNumber) != (nil)))) && bool(bool((proposedWindowSize) != (nil)))))) {
return nil, errors.WithStack(utils.ParseValidationError{Message: "service request should be set"})
}
diff --git a/plc4go/protocols/bacnetip/readwrite/model/BACnetTagPayloadSignedInteger.go b/plc4go/protocols/bacnetip/readwrite/model/BACnetTagPayloadSignedInteger.go
index af93cfa04e..024a054611 100644
--- a/plc4go/protocols/bacnetip/readwrite/model/BACnetTagPayloadSignedInteger.go
+++ b/plc4go/protocols/bacnetip/readwrite/model/BACnetTagPayloadSignedInteger.go
@@ -715,8 +715,8 @@ func (m *_BACnetTagPayloadSignedInteger) parse(ctx context.Context, readBuffer u
m.ValueInt64 = valueInt64
// Validation
- if !(bool(bool(bool(bool(bool(bool(bool(isInt8) || bool(isInt16)) || bool(isInt24)) || bool(isInt32)) || bool(isInt40)) || bool(isInt48)) || bool(isInt56)) || bool(isInt64)) {
- return nil, errors.WithStack(utils.ParseValidationError{Message: "unmapped integer length"})
+ if !(bool(bool(bool(bool(bool(bool(bool((bool(isInt8) && bool(bool((valueInt8) != (nil))))) || bool((bool(isInt16) && bool(bool((valueInt16) != (nil)))))) || bool((bool(isInt24) && bool(bool((valueInt24) != (nil)))))) || bool((bool(isInt32) && bool(bool((valueInt32) != (nil)))))) || bool((bool(isInt40) && bool(bool((valueInt40) != (nil)))))) || bool((bool(isInt48) && bool(bool((valueInt48) != (nil)))))) || bool((bool(isInt56) && bool(bool((valueInt56) != (nil)))))) || bool((bool(isInt64 [...]
+ return nil, errors.WithStack(utils.ParseValidationError{Message: "unmapped or truncated integer length"})
}
actualValue, err := ReadVirtualField[int64](ctx, "actualValue", (*int64)(nil), utils.InlineIf(isInt8, func() any { return int64((*valueInt8)) }, func() any {
diff --git a/plc4go/protocols/bacnetip/readwrite/model/BACnetTagPayloadUnsignedInteger.go b/plc4go/protocols/bacnetip/readwrite/model/BACnetTagPayloadUnsignedInteger.go
index ff7cba5524..9a0736e00b 100644
--- a/plc4go/protocols/bacnetip/readwrite/model/BACnetTagPayloadUnsignedInteger.go
+++ b/plc4go/protocols/bacnetip/readwrite/model/BACnetTagPayloadUnsignedInteger.go
@@ -715,8 +715,8 @@ func (m *_BACnetTagPayloadUnsignedInteger) parse(ctx context.Context, readBuffer
m.ValueUint64 = valueUint64
// Validation
- if !(bool(bool(bool(bool(bool(bool(bool(isUint8) || bool(isUint16)) || bool(isUint24)) || bool(isUint32)) || bool(isUint40)) || bool(isUint48)) || bool(isUint56)) || bool(isUint64)) {
- return nil, errors.WithStack(utils.ParseValidationError{Message: "unmapped integer length"})
+ if !(bool(bool(bool(bool(bool(bool(bool((bool(isUint8) && bool(bool((valueUint8) != (nil))))) || bool((bool(isUint16) && bool(bool((valueUint16) != (nil)))))) || bool((bool(isUint24) && bool(bool((valueUint24) != (nil)))))) || bool((bool(isUint32) && bool(bool((valueUint32) != (nil)))))) || bool((bool(isUint40) && bool(bool((valueUint40) != (nil)))))) || bool((bool(isUint48) && bool(bool((valueUint48) != (nil)))))) || bool((bool(isUint56) && bool(bool((valueUint56) != (nil)))))) || bool [...]
+ return nil, errors.WithStack(utils.ParseValidationError{Message: "unmapped or truncated integer length"})
}
actualValue, err := ReadVirtualField[uint64](ctx, "actualValue", (*uint64)(nil), utils.InlineIf(isUint8, func() any { return uint64((*valueUint8)) }, func() any {
diff --git a/plc4go/protocols/bacnetip/readwrite/model/BACnetTruncatedOptional_test.go b/plc4go/protocols/bacnetip/readwrite/model/BACnetTruncatedOptional_test.go
new file mode 100644
index 0000000000..0d4c544fec
--- /dev/null
+++ b/plc4go/protocols/bacnetip/readwrite/model/BACnetTruncatedOptional_test.go
@@ -0,0 +1,70 @@
+/*
+ * 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 model
+
+import (
+ "context"
+ "testing"
+
+ "github.com/apache/plc4x/plc4go/spi/utils"
+)
+
+// An optional field is read only when its condition holds, but the read can still come up empty on
+// a datagram that ends early: ReadOptionalField reports "not enough bytes" as an absent field.
+// Anything deriving a value from such a field therefore has to require it to be present, or the
+// derivation dereferences a nil pointer. Each case below announces a field and then stops before it.
+func TestTruncatedOptionalIsRejectedNotDereferenced(t *testing.T) {
+ ctx := context.Background()
+ buf := func(b ...byte) utils.ReadBufferByteBased { return utils.NewReadBufferByteBased(b) }
+
+ tests := []struct {
+ name string
+ parse func() (any, error)
+ }{
+ // An actualLength of 1 announces a one byte payload that is not there.
+ {"unsigned integer payload", func() (any, error) {
+ return BACnetTagPayloadUnsignedIntegerParseWithBuffer(ctx, buf(), uint32(1))
+ }},
+ {"signed integer payload", func() (any, error) {
+ return BACnetTagPayloadSignedIntegerParseWithBuffer(ctx, buf(), uint32(1))
+ }},
+ // A segmented APDU announces a sequence number and a proposed window size after the
+ // invoke id; both are missing here.
+ {"segmented complex ack", func() (any, error) {
+ return APDUParseWithBuffer[APDU](ctx, buf(0x38, 0x01), uint16(2))
+ }},
+ {"segmented confirmed request", func() (any, error) {
+ return APDUParseWithBuffer[APDU](ctx, buf(0x08, 0x00, 0x01), uint16(3))
+ }},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ defer func() {
+ if r := recover(); r != nil {
+ t.Fatalf("parsing panicked instead of returning an error: %v", r)
+ }
+ }()
+ if _, err := test.parse(); err == nil {
+ t.Error("expected a truncated message to be rejected")
+ }
+ })
+ }
+}
diff --git a/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/APDUComplexAck.java b/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/APDUComplexAck.java
index ca5162e79c..8e06ad5a0d 100644
--- a/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/APDUComplexAck.java
+++ b/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/APDUComplexAck.java
@@ -171,7 +171,7 @@ public class APDUComplexAck extends APDU implements Message {
BACnetServiceAck serviceAck = FieldReaderFactory.readOptionalField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(BACnetServiceAck.class, BACnetServiceAck.staticParse(readBuffer, (long) ((apduLength) - (apduHeaderReduction)))), readBuffer), !(segmentedMessage), WithOption.WithName("serviceAck"));
// Validation Field
- if(!((((!(segmentedMessage)) && ((serviceAck) != (null)))) || (segmentedMessage))) {
+ if(!((((!(segmentedMessage)) && ((serviceAck) != (null)))) || ((((segmentedMessage) && ((sequenceNumber) != (null))) && ((proposedWindowSize) != (null)))))) {
throw new ParseAssertException("service ack should be set");
}
diff --git a/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/APDUConfirmedRequest.java b/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/APDUConfirmedRequest.java
index 63523e2906..63994bd727 100644
--- a/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/APDUConfirmedRequest.java
+++ b/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/APDUConfirmedRequest.java
@@ -212,7 +212,7 @@ public class APDUConfirmedRequest extends APDU implements Message {
BACnetConfirmedServiceRequest serviceRequest = FieldReaderFactory.readOptionalField(DataReaderFactory.readComplex(() -> DataReaderFactory.castToDeclaredType(BACnetConfirmedServiceRequest.class, BACnetConfirmedServiceRequest.staticParse(readBuffer, (long) ((apduLength) - (apduHeaderReduction)))), readBuffer), !(segmentedMessage), WithOption.WithName("serviceRequest"));
// Validation Field
- if(!((((!(segmentedMessage)) && ((serviceRequest) != (null)))) || (segmentedMessage))) {
+ if(!((((!(segmentedMessage)) && ((serviceRequest) != (null)))) || ((((segmentedMessage) && ((sequenceNumber) != (null))) && ((proposedWindowSize) != (null)))))) {
throw new ParseAssertException("service request should be set");
}
diff --git a/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/BACnetTagPayloadSignedInteger.java b/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/BACnetTagPayloadSignedInteger.java
index 815841c6a2..70bf72dd4e 100644
--- a/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/BACnetTagPayloadSignedInteger.java
+++ b/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/BACnetTagPayloadSignedInteger.java
@@ -244,8 +244,8 @@ public class BACnetTagPayloadSignedInteger implements Message {
Long valueInt64 = FieldReaderFactory.readOptionalField(DataReaderFactory.readSignedLong(readBuffer, 64), isInt64, WithOption.WithName("valueInt64"));
// Validation Field
- if(!((((((((isInt8) || (isInt16)) || (isInt24)) || (isInt32)) || (isInt40)) || (isInt48)) || (isInt56)) || (isInt64))) {
- throw new ParseAssertException("unmapped integer length");
+ if(!((((((((((isInt8) && ((valueInt8) != (null)))) || (((isInt16) && ((valueInt16) != (null))))) || (((isInt24) && ((valueInt24) != (null))))) || (((isInt32) && ((valueInt32) != (null))))) || (((isInt40) && ((valueInt40) != (null))))) || (((isInt48) && ((valueInt48) != (null))))) || (((isInt56) && ((valueInt56) != (null))))) || (((isInt64) && ((valueInt64) != (null)))))) {
+ throw new ParseAssertException("unmapped or truncated integer length");
}
// Virtual Field: actualValue (doesn't parse anything, just makes the value available)
diff --git a/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/BACnetTagPayloadUnsignedInteger.java b/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/BACnetTagPayloadUnsignedInteger.java
index 5bcf2b30be..b7e1a9e6a2 100644
--- a/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/BACnetTagPayloadUnsignedInteger.java
+++ b/plc4j/drivers/bacnet/src/main/generated/org/apache/plc4x/java/bacnetip/readwrite/BACnetTagPayloadUnsignedInteger.java
@@ -250,8 +250,8 @@ public class BACnetTagPayloadUnsignedInteger implements Message {
BigInteger valueUint64 = FieldReaderFactory.readOptionalField(DataReaderFactory.readUnsignedBigInteger(readBuffer, 64), isUint64, WithOption.WithName("valueUint64"));
// Validation Field
- if(!((((((((isUint8) || (isUint16)) || (isUint24)) || (isUint32)) || (isUint40)) || (isUint48)) || (isUint56)) || (isUint64))) {
- throw new ParseAssertException("unmapped integer length");
+ if(!((((((((((isUint8) && ((valueUint8) != (null)))) || (((isUint16) && ((valueUint16) != (null))))) || (((isUint24) && ((valueUint24) != (null))))) || (((isUint32) && ((valueUint32) != (null))))) || (((isUint40) && ((valueUint40) != (null))))) || (((isUint48) && ((valueUint48) != (null))))) || (((isUint56) && ((valueUint56) != (null))))) || (((isUint64) && ((valueUint64) != (null)))))) {
+ throw new ParseAssertException("unmapped or truncated integer length");
}
// Virtual Field: actualValue (doesn't parse anything, just makes the value available)
diff --git a/protocols/bacnetip/src/main/resources/protocols/bacnetip/bacnet-tags.mspec b/protocols/bacnetip/src/main/resources/protocols/bacnetip/bacnet-tags.mspec
index 8a97c58639..97573af426 100644
--- a/protocols/bacnetip/src/main/resources/protocols/bacnetip/bacnet-tags.mspec
+++ b/protocols/bacnetip/src/main/resources/protocols/bacnetip/bacnet-tags.mspec
@@ -226,7 +226,9 @@
[optional uint 56 valueUint56 'isUint56' ]
[virtual bit isUint64 'actualLength == 8' ]
[optional uint 64 valueUint64 'isUint64' ]
- [validation 'isUint8 || isUint16 || isUint24 || isUint32 || isUint40 || isUint48 || isUint56 || isUint64' "unmapped integer length"]
+ [validation '(isUint8 && valueUint8 != null) || (isUint16 && valueUint16 != null) || (isUint24 && valueUint24 != null)
+ || (isUint32 && valueUint32 != null) || (isUint40 && valueUint40 != null) || (isUint48 && valueUint48 != null)
+ || (isUint56 && valueUint56 != null) || (isUint64 && valueUint64 != null)' "unmapped or truncated integer length"]
[virtual uint 64 actualValue 'isUint8?valueUint8:(isUint16?valueUint16:(isUint24?valueUint24:(isUint32?valueUint32:(isUint40?valueUint40:(isUint48?valueUint48:(isUint56?valueUint56:valueUint64))))))']
]
@@ -248,7 +250,9 @@
[optional int 56 valueInt56 'isInt56' ]
[virtual bit isInt64 'actualLength == 8' ]
[optional int 64 valueInt64 'isInt64' ]
- [validation 'isInt8 || isInt16 || isInt24 || isInt32 || isInt40 || isInt48 || isInt56 || isInt64' "unmapped integer length"]
+ [validation '(isInt8 && valueInt8 != null) || (isInt16 && valueInt16 != null) || (isInt24 && valueInt24 != null)
+ || (isInt32 && valueInt32 != null) || (isInt40 && valueInt40 != null) || (isInt48 && valueInt48 != null)
+ || (isInt56 && valueInt56 != null) || (isInt64 && valueInt64 != null)' "unmapped or truncated integer length"]
[virtual int 64 actualValue 'isInt8?valueInt8:(isInt16?valueInt16:(isInt24?valueInt24:(isInt32?valueInt32:(isInt40?valueInt40:(isInt48?valueInt48:(isInt56?valueInt56:valueInt64))))))']
]
diff --git a/protocols/bacnetip/src/main/resources/protocols/bacnetip/bacnetip.mspec b/protocols/bacnetip/src/main/resources/protocols/bacnetip/bacnetip.mspec
index ab2ef4fdc4..e6dc469c2b 100644
--- a/protocols/bacnetip/src/main/resources/protocols/bacnetip/bacnetip.mspec
+++ b/protocols/bacnetip/src/main/resources/protocols/bacnetip/bacnetip.mspec
@@ -302,7 +302,8 @@
'3 + (segmentedMessage ? 2 : 0)' ]
[optional BACnetConfirmedServiceRequest('apduLength - apduHeaderReduction')
serviceRequest '!segmentedMessage' ]
- [validation '(!segmentedMessage && serviceRequest != null) || segmentedMessage'
+ [validation '(!segmentedMessage && serviceRequest != null)
+ || (segmentedMessage && sequenceNumber != null && proposedWindowSize != null)'
"service request should be set" ]
// When we read the first segment we want the service choice to be part of the bytes so we only read it > 0
[optional BACnetConfirmedServiceChoice
@@ -336,7 +337,8 @@
'2 + (segmentedMessage ? 2 : 0)' ]
[optional BACnetServiceAck('apduLength - apduHeaderReduction')
serviceAck '!segmentedMessage' ]
- [validation '(!segmentedMessage && serviceAck != null) || segmentedMessage'
+ [validation '(!segmentedMessage && serviceAck != null)
+ || (segmentedMessage && sequenceNumber != null && proposedWindowSize != null)'
"service ack should be set" ]
// When we read the first segment we want the service choice to be part of the bytes so we only read it > 0
[optional BACnetConfirmedServiceChoice