Skip to content

Commit

Permalink
Fixing the issue where multi-byte characters are split in writeCData(…
Browse files Browse the repository at this point in the history
…) if first byte sits right at the end of the buffer (#87)
  • Loading branch information
tatsel authored Jun 7, 2024
1 parent e45c2eb commit 14b58b9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/fasterxml/aalto/out/ByteXmlWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,13 @@ public int writeCData(char[] cbuf, int offset, int len)
protected int writeCDataContents(char[] cbuf, int offset, int len)
throws IOException, XMLStreamException
{
if (_surrogate != 0) {
outputSurrogates(_surrogate, cbuf[offset]);
// reset the temporary surrogate storage
_surrogate = 0;
++offset;
--len;
}
/* Unlike with writeCharacters() and fastWriteName(), let's not
* worry about split buffers here: this is unlikely to become
* performance bottleneck. This allows keeping it simple; and
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/fasterxml/aalto/sax/TestSaxWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,29 @@ public void testSurrogateMemory2() throws Exception {
writer.writeEndTag(writer.constructName("testelement"));
writer.close(false);
}

public void testSurrogateMemory3() throws Exception {
// This test aims to produce the
// javax.xml.stream.XMLStreamException: Incomplete surrogate pair in content: first char 0xdfce, second 0x78
// error message. The issue was similar to the one described in testSurrogateMemory1(), except it happened in
// ByteXmlWriter#writeCDataContents(), where check for existing _surrogate was missing prior to the fix,
// as opposed to ByteXmlWriter#writeCharacters().
StringBuilder testText = new StringBuilder();
for (int i = 0; i < 511; i++)
testText.append('x');
testText.append("\uD835\uDFCE");
for (int i = 0; i < 512; i++)
testText.append('x');

WriterConfig writerConfig = new WriterConfig();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Utf8XmlWriter writer = new Utf8XmlWriter(writerConfig, byteArrayOutputStream);
writer.writeStartTagStart(writer.constructName("testelement"));
writer.writeCData(testText.toString());
writer.writeStartTagEnd();
writer.writeEndTag(writer.constructName("testelement"));
writer.close(false);

}

}

0 comments on commit 14b58b9

Please sign in to comment.