This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch 3.6.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
commit 4b885c43b9b29a01dba688bbf30feb203a396dbe Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Wed Nov 25 14:29:36 2026 +0000 Pop the inner element counter in DepthRestrictingStreamReader.getElementText (#3777) (cherry picked from commit 6338ef5c994d1387e4768d1e6ce52d54cb28ee22) --- .../staxutils/DepthRestrictingStreamReader.java | 10 ++++++++ .../DepthRestrictingStreamReaderTest.java | 29 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/cxf/staxutils/DepthRestrictingStreamReader.java b/core/src/main/java/org/apache/cxf/staxutils/DepthRestrictingStreamReader.java index c9dc1fd5497..809ab69aced 100644 --- a/core/src/main/java/org/apache/cxf/staxutils/DepthRestrictingStreamReader.java +++ b/core/src/main/java/org/apache/cxf/staxutils/DepthRestrictingStreamReader.java @@ -82,4 +82,14 @@ public class DepthRestrictingStreamReader extends DepthXMLStreamReader { } return next; } + + @Override + public String getElementText() throws XMLStreamException { + // getElementText() consumes the END_ELEMENT without going through next() + String text = super.getElementText(); + if (props.getInnerElementCountThreshold() != -1 && !stack.isEmpty()) { + stack.pop(); + } + return text; + } } diff --git a/core/src/test/java/org/apache/cxf/staxutils/DepthRestrictingStreamReaderTest.java b/core/src/test/java/org/apache/cxf/staxutils/DepthRestrictingStreamReaderTest.java index d9333d40786..d1230f5182b 100644 --- a/core/src/test/java/org/apache/cxf/staxutils/DepthRestrictingStreamReaderTest.java +++ b/core/src/test/java/org/apache/cxf/staxutils/DepthRestrictingStreamReaderTest.java @@ -20,12 +20,15 @@ package org.apache.cxf.staxutils; import java.io.ByteArrayOutputStream; +import java.io.StringReader; import javax.xml.stream.XMLStreamReader; import org.junit.Test; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class DepthRestrictingStreamReaderTest { @@ -116,4 +119,28 @@ public class DepthRestrictingStreamReaderTest { 2); StaxUtils.copy(dr, new ByteArrayOutputStream()); } -} \ No newline at end of file + + @Test + public void testInnerElementCountExceededWithGetElementText() throws Exception { + StringBuilder sb = new StringBuilder("<root>"); + for (int i = 0; i < 10; i++) { + sb.append("<child>").append(i).append("</child>"); + } + sb.append("</root>"); + XMLStreamReader reader = StaxUtils.createXMLStreamReader(new StringReader(sb.toString())); + + DepthRestrictingStreamReader dr = new DepthRestrictingStreamReader(reader, -1, -1, 5); + int read = 0; + try { + while (dr.hasNext()) { + if (dr.next() == XMLStreamReader.START_ELEMENT && "child".equals(dr.getLocalName())) { + assertEquals(Integer.toString(read), dr.getElementText()); + read++; + } + } + fail("Expected DepthExceededStaxException after " + read + " children"); + } catch (DepthExceededStaxException ex) { + assertEquals(4, read); + } + } +}
