Skip to content

Fix getTextCharacters to honor sourceStart offset#36914

Open
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/xmlstreamreader-gettextcharacters
Open

Fix getTextCharacters to honor sourceStart offset#36914
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/xmlstreamreader-gettextcharacters

Conversation

@junhyeong9812

Copy link
Copy Markdown

Overview

AbstractXMLStreamReader (the base for XMLEventStreamReader, reachable via the public
StaxUtils.createEventStreamReader(XMLEventReader)) implements
getTextCharacters(int sourceStart, char[] target, int targetStart, int length).

Problem

The copy length was capped with Math.min(length, source.length), ignoring sourceStart:

char[] source = getTextCharacters();
length = Math.min(length, source.length);
System.arraycopy(source, sourceStart, target, targetStart, length);

When sourceStart > 0 and sourceStart + length exceeds the text length, System.arraycopy
reads past the end of source and throws ArrayIndexOutOfBoundsException. This breaks the
XMLStreamReader#getTextCharacters(int, char[], int, int) contract, which copies up to length
characters starting at sourceStart and returns the number actually copied — exactly the pattern
a consumer uses to read a long text node in chunks (sourceStart += copied).

Fix

Cap the length by the number of characters remaining from sourceStart
(Math.min(length, source.length - sourceStart)). A regression test is added to
XMLEventStreamReaderTests.

AbstractXMLStreamReader.getTextCharacters(int sourceStart, char[],
int, int) capped the copy length with Math.min(length, source.length),
ignoring sourceStart. When sourceStart > 0 and sourceStart + length
exceeds the text length, System.arraycopy read past the end of the
source array and threw ArrayIndexOutOfBoundsException, contrary to the
XMLStreamReader#getTextCharacters contract (copy up to length
characters starting at sourceStart and return the number copied).

Cap the length by the number of characters remaining from sourceStart.

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants