Skip to content

Commit d58db2a

Browse files
committed
8349699: XSL transform fails with certain UTF-8 characters on 1024 byte boundaries
1 parent ff52859 commit d58db2a

File tree

2 files changed

+32
-5
lines changed
  • src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer
  • test/jaxp/javax/xml/jaxp/unittest/transform

2 files changed

+32
-5
lines changed

Diff for: src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToStream.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2006, 2025, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -55,7 +55,7 @@
5555
* serializers (xml, html, text ...) that write output to a stream.
5656
*
5757
* @xsl.usage internal
58-
* @LastModified: Mar 2022
58+
* @LastModified: Feb 2025
5959
*/
6060
abstract public class ToStream extends SerializerBase {
6161

@@ -955,7 +955,7 @@ protected int writeUTF16Surrogate(char c, char ch[], int i, int end)
955955
throws IOException, SAXException
956956
{
957957
int status = -1;
958-
if (i + 1 >= end)
958+
if (i + 1 >= end && m_highSurrogate == 0)
959959
{
960960
m_highSurrogate = c;
961961
return status;

Diff for: test/jaxp/javax/xml/jaxp/unittest/transform/JDK8207760.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141

4242
/*
4343
* @test
44+
* @bug 8207760 8349699
45+
* @summary Verifies that a surrogate pair at the edge of a buffer is properly handled
4446
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
4547
* @run testng/othervm transform.JDK8207760
46-
* @summary Verifies that a surrogate pair at the edge of a buffer is properly handled
47-
* @bug 8207760
4848
*/
4949
public class JDK8207760 {
5050
final String xsl8207760 =
@@ -101,6 +101,33 @@ public Object[][] getDataBug8207760_cdata() {
101101
};
102102
}
103103

104+
/*
105+
* @bug 8349699
106+
* Verifies that a surrogate pair at the edge of a buffer is properly handled
107+
* when serializing into a Character section.
108+
*/
109+
@Test
110+
public final void testBug8349699() throws Exception {
111+
String xs = "x".repeat(1017);
112+
String expected = xs + "\uD835\uDF03\uD835\uDF00\uD835\uDF00\uD835\uDF00\uD835\uDF00";
113+
String xml = "<?xml version=\"1.0\" ?><a>{1017x}\uD835\uDF03\uD835\uDF00\uD835\uDF00<b>\uD835\uDF00</b>\uD835\uDF00</a> "
114+
.replace("{1017x}", xs);
115+
String xsl = """
116+
<?xml version="1.0" encoding="UTF-8"?>
117+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
118+
<xsl:output encoding="UTF-8" method="text" />
119+
<xsl:template match="/"><xsl:apply-templates select="node()" /></xsl:template>
120+
</xsl:stylesheet>
121+
""";
122+
123+
Transformer t = createTransformerFromInputstream(
124+
new ByteArrayInputStream(xsl.getBytes(StandardCharsets.UTF_8)));
125+
//t.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
126+
StringWriter sw = new StringWriter();
127+
t.transform(new StreamSource(new StringReader(xml)), new StreamResult(sw));
128+
Assert.assertEquals(sw.toString(), expected);
129+
}
130+
104131
/*
105132
* @bug 8207760
106133
* Verifies that a surrogate pair at the edge of a buffer is properly handled

0 commit comments

Comments
 (0)