Skip to content

Commit 46b7efc

Browse files
committed
Fix #91: handle split-buffary surrogate for comments as well
1 parent 0f6af6d commit 46b7efc

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

release-notes/VERSION

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ Project: aalto-xml
66

77
1.3.3 (not yet released)
88

9-
#86: Multi-byte characters are split in writeCData() if first byte sits
9+
#86: Multi-byte characters are split in `writeCData()` if first byte sits
1010
right at the end of the buffer
1111
(reported, fix contributed by @tatsel)
1212
#90: Update stax2-api dep to 4.2.2 (from 4.2)
13+
#91: Multi-byte characters are split in `writeComment()` if first byte sits
14+
right at the end of the buffer
1315

1416
1.3.2 (25-Apr-2022)
1517

src/main/java/com/fasterxml/aalto/out/ByteXmlWriter.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ protected int writeCDataContents(char[] cbuf, int offset, int len)
799799
++offset;
800800
--len;
801801
}
802+
802803
// Unlike with writeCharacters() and fastWriteName(), let's not
803804
// worry about split buffers here: this is unlikely to become
804805
// performance bottleneck. This allows keeping it simple; and
@@ -1197,12 +1198,18 @@ public int writeComment(String data) throws IOException, XMLStreamException
11971198
protected int writeCommentContents(char[] cbuf, int offset, int len)
11981199
throws IOException, XMLStreamException
11991200
{
1200-
/* Unlike with writeCharacters() and fastWriteName(), let's not
1201-
* worry about split buffers here: this is unlikely to become
1202-
* performance bottleneck. This allows keeping it simple; and
1203-
* should it matter, we could start doing fast version here
1204-
* as well.
1205-
*/
1201+
if (_surrogate != 0) {
1202+
outputSurrogates(_surrogate, cbuf[offset]);
1203+
// reset the temporary surrogate storage
1204+
_surrogate = 0;
1205+
++offset;
1206+
--len;
1207+
}
1208+
1209+
// Unlike with writeCharacters() and fastWriteName(), let's not
1210+
// worry about split buffers here: this is unlikely to become
1211+
// performance bottleneck. This allows keeping it simple; and
1212+
// should it matter, we could start doing fast version here as well.
12061213
len += offset; // now marks the end
12071214

12081215
main_loop:

src/test/java/com/fasterxml/aalto/sax/TestSaxWriter.java

+24-5
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ public void testSplitSurrogateWithAttributeValue2() throws Exception
6060

6161
public void testSplitSurrogateWithCData() throws Exception
6262
{
63-
// This test aims to produce the
64-
// javax.xml.stream.XMLStreamException: Incomplete surrogate pair in content: first char 0xdfce, second 0x78
65-
// error message. The issue was similar to the one described in testSurrogateMemory1(), except it happened in
66-
// ByteXmlWriter#writeCDataContents(), where check for existing _surrogate was missing prior to the fix,
67-
// as opposed to ByteXmlWriter#writeCharacters().
63+
// Modification of "testSplitSurrogateWithAttributeValue()" but for CDATA
6864
StringBuilder testText = new StringBuilder();
6965
for (int i = 0; i < 511; i++) {
7066
testText.append('x');
@@ -83,4 +79,27 @@ public void testSplitSurrogateWithCData() throws Exception
8379
writer.writeEndTag(writer.constructName("testelement"));
8480
writer.close(false);
8581
}
82+
83+
84+
public void testSplitSurrogateWithComment() throws Exception
85+
{
86+
// Modification of "testSplitSurrogateWithAttributeValue()" but for Comment
87+
StringBuilder testText = new StringBuilder();
88+
for (int i = 0; i < 511; i++) {
89+
testText.append('x');
90+
}
91+
testText.append("\uD835\uDFCE");
92+
for (int i = 0; i < 512; i++) {
93+
testText.append('x');
94+
}
95+
96+
WriterConfig writerConfig = new WriterConfig();
97+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
98+
Utf8XmlWriter writer = new Utf8XmlWriter(writerConfig, byteArrayOutputStream);
99+
writer.writeStartTagStart(writer.constructName("testelement"));
100+
writer.writeComment(testText.toString());
101+
writer.writeStartTagEnd();
102+
writer.writeEndTag(writer.constructName("testelement"));
103+
writer.close(false);
104+
}
86105
}

0 commit comments

Comments
 (0)