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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new ffa7f0bbfd0e CAMEL-25137: Fix ArrayIndexOutOfBoundsException in MLLP 
consumer when message starts with segment delimiter (#27136)
ffa7f0bbfd0e is described below

commit ffa7f0bbfd0e9fdc9b2b3c05162a69f3d1de740b
Author: Torsten Mielke <[email protected]>
AuthorDate: Mon Oct 5 10:40:44 2026 +0200

    CAMEL-25137: Fix ArrayIndexOutOfBoundsException in MLLP consumer when 
message starts with segment delimiter (#27136)
    
    Guard against i == 0 before accessing hl7MessageBytes[i - 1] in
    populateHl7DataHeaders, and treat endOfMSH == 0 as invalid (same as -1)
    so a zero-length MSH does not fall through to the header parsing branch.
    
    Co-Authored-By: Claude Opus 4.6 <[email protected]>
---
 .../component/mllp/MllpTcpServerConsumer.java      |  4 +-
 ...cpServerConsumerPopulateHl7DataHeadersTest.java | 85 ++++++++++++++++++++++
 2 files changed, 87 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
 
b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
index 950e1a4f7fb5..edb576b07795 100644
--- 
a/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
+++ 
b/components/camel-mllp/src/main/java/org/apache/camel/component/mllp/MllpTcpServerConsumer.java
@@ -392,7 +392,7 @@ public class MllpTcpServerConsumer extends DefaultConsumer {
                     fieldSeparatorIndexes.add(i);
                 } else if (MllpProtocolConstants.SEGMENT_DELIMITER == 
hl7MessageBytes[i]) {
                     // If the MSH Segment doesn't have a trailing field 
separator, add one so the field can be extracted into a header
-                    if (fieldSeparator != hl7MessageBytes[i - 1]) {
+                    if (i > 0 && fieldSeparator != hl7MessageBytes[i - 1]) {
                         fieldSeparatorIndexes.add(i);
                     }
                     endOfMSH = i;
@@ -400,7 +400,7 @@ public class MllpTcpServerConsumer extends DefaultConsumer {
                 }
             }
 
-            if (-1 == endOfMSH) {
+            if (endOfMSH <= 0) {
                 // TODO:  May want to throw some sort of an Exception here
                 log.warn("Population of message headers failed - unable to 
find the end of the MSH segment");
             } else {
diff --git 
a/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerPopulateHl7DataHeadersTest.java
 
b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerPopulateHl7DataHeadersTest.java
new file mode 100644
index 000000000000..fb976cb54941
--- /dev/null
+++ 
b/components/camel-mllp/src/test/java/org/apache/camel/component/mllp/MllpTcpServerConsumerPopulateHl7DataHeadersTest.java
@@ -0,0 +1,85 @@
+/*
+ * 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
+ *
+ *      http://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 org.apache.camel.component.mllp;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+public class MllpTcpServerConsumerPopulateHl7DataHeadersTest extends 
CamelTestSupport {
+
+    private MllpTcpServerConsumer consumer;
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    protected void doPreSetup() throws Exception {
+        MllpComponent mllpComponent = 
createCamelContext().getComponent("mllp", MllpComponent.class);
+        MllpEndpoint endpoint = (MllpEndpoint) 
mllpComponent.createEndpoint("mllp://localhost:0");
+        consumer = new MllpTcpServerConsumer(endpoint, exchange -> {
+        });
+    }
+
+    @Test
+    void testMessageStartingWithSegmentDelimiter() {
+        // Malformed payload: leading segment delimiter (\r) before an 
otherwise valid MSH segment.
+        // byte[0] = 0x0D (SEGMENT_DELIMITER), byte[3] = 'H' (not 0x0D), so 
fieldSeparator = 'H'.
+        // At i=0 the else-if branch would access hl7MessageBytes[i-1] = 
hl7MessageBytes[-1].
+        byte[] malformedMessage
+                = 
"\rMSH|^~\\&|ADT|EPIC|JCAPS|CC|20160902123950|RISTECH|ADT^A08|00001|D|2.3\r".getBytes();
+
+        Exchange exchange = new DefaultExchange(context);
+        Message message = exchange.getIn();
+
+        assertDoesNotThrow(
+                () -> consumer.populateHl7DataHeaders(exchange, message, 
malformedMessage),
+                "populateHl7DataHeaders should handle a leading segment 
delimiter gracefully");
+
+        assertNull(message.getHeader(MllpConstants.MLLP_SENDING_APPLICATION),
+                "No HL7 headers should be set for a malformed message");
+        assertNull(message.getHeader(MllpConstants.MLLP_MESSAGE_TYPE),
+                "No HL7 headers should be set for a malformed message");
+    }
+
+    @Test
+    void testFieldSeparatorEqualsSegmentDelimiter() {
+        // When byte[3] is also 0x0D, fieldSeparator == SEGMENT_DELIMITER.
+        // Every 0x0D byte matches the first if-branch (fieldSeparator check),
+        // so the else-if (SEGMENT_DELIMITER) is never entered and endOfMSH 
stays -1.
+        byte[] malformedMessage = "XX\r\rYYYYYYYY".getBytes();
+
+        Exchange exchange = new DefaultExchange(context);
+        Message message = exchange.getIn();
+
+        assertDoesNotThrow(
+                () -> consumer.populateHl7DataHeaders(exchange, message, 
malformedMessage),
+                "populateHl7DataHeaders should handle fieldSeparator == 
SEGMENT_DELIMITER gracefully");
+
+        assertNull(message.getHeader(MllpConstants.MLLP_SENDING_APPLICATION),
+                "No HL7 headers should be set when MSH end is not found");
+        assertNull(message.getHeader(MllpConstants.MLLP_MESSAGE_TYPE),
+                "No HL7 headers should be set when MSH end is not found");
+    }
+}

Reply via email to