From 2504bde5d69fcb64fd2f7469a6c15707cc4984d2 Mon Sep 17 00:00:00 2001 From: Sangwook Ha Date: Thu, 2 Nov 2023 01:40:52 -0700 Subject: [PATCH] Update buffer readerIndex after XML parsing error The readerIndex of the input buffer may have not reached the end of the message when XML parsing fails. And in this case unnecessary decoding may get repeatedly triggered for the unread partial message. Make sure that readerIndex advances to writerIndex so that the next decoding can start for a new message and add a test case to show this parsing error scenario. It means that we can skip all readable bytes counted by: (ByteBuff.writerIndex - ByteBuff.readerIndex). JIRA: NETCONF-1194 Change-Id: I01b668eb0c995ff01435e983cdbba1490645909e Signed-off-by: Sangwook Ha Signed-off-by: Ivan Hrasko --- .../handler/NetconfXMLToMessageDecoder.java | 1 + .../NetconfXMLToMessageDecoderTest.java | 23 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfXMLToMessageDecoder.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfXMLToMessageDecoder.java index 9244d8ee19..0a1e44f264 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfXMLToMessageDecoder.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfXMLToMessageDecoder.java @@ -74,6 +74,7 @@ public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder { } catch (SAXParseException e) { LOG.error("Failed to parse received message", e); out.add(e); + in.skipBytes(in.readableBytes()); } } else { LOG.debug("No more content in incoming buffer."); diff --git a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java index 44f718807f..6a0d79f0d0 100644 --- a/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java +++ b/netconf/netconf-netty-util/src/test/java/org/opendaylight/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java @@ -7,12 +7,15 @@ */ package org.opendaylight.netconf.nettyutil.handler; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import io.netty.buffer.Unpooled; import java.util.ArrayList; import org.junit.Test; +import org.opendaylight.netconf.api.messages.NetconfMessage; import org.xml.sax.SAXParseException; public class NetconfXMLToMessageDecoderTest { @@ -89,5 +92,23 @@ public class NetconfXMLToMessageDecoderTest { out); assertEquals(1, out.size()); } -} + @Test + public void testDecodeAfterInvalidXml() throws Exception { + /* Test that decoding of the next message after an invalid XML is successful. + */ + final var out = new ArrayList<>(); + final var decoder = new NetconfXMLToMessageDecoder(); + final var buffer = Unpooled.buffer(); + + buffer.writeBytes("".getBytes()); + decoder.decode(null, buffer, out); + assertEquals(1, out.size()); + assertThat(out.get(0), instanceOf(SAXParseException.class)); + + buffer.writeBytes("".getBytes()); + decoder.decode(null, buffer, out); + assertEquals(2, out.size()); + assertThat(out.get(1), instanceOf(NetconfMessage.class)); + } +} -- 2.36.6