Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public char[] getTextCharacters() {
@Override
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) {
char[] source = getTextCharacters();
length = Math.min(length, source.length);
length = Math.min(length, source.length - sourceStart);
System.arraycopy(source, sourceStart, target, targetStart, length);
return length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
Expand Down Expand Up @@ -68,4 +69,20 @@ void readCorrect() throws Exception {
assertThat(XmlContent.from(writer)).isSimilarTo(XML, nodeFilter);
}

@Test // getTextCharacters(sourceStart, ...) must not read past the source
void getTextCharactersHonorsSourceStart() throws Exception {
advanceToCharacters();
// text node is "content" (7 chars); copy from index 4 with an oversized buffer
char[] target = new char[10];
int count = streamReader.getTextCharacters(4, target, 0, 10);
assertThat(count).isEqualTo(3);
assertThat(new String(target, 0, count)).isEqualTo("ent");
}

private void advanceToCharacters() throws Exception {
while (streamReader.getEventType() != XMLStreamConstants.CHARACTERS) {
streamReader.next();
}
}

}