Skip to content

Commit ee2974f

Browse files
author
duke
committedMar 25, 2022
Automatic merge of jdk:master into master
2 parents ab9f32e + f4fd53d commit ee2974f

File tree

4 files changed

+194
-15
lines changed

4 files changed

+194
-15
lines changed
 

‎src/java.xml/share/classes/com/sun/org/apache/xml/internal/serializer/ToStream.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2006, 2022, Oracle and/or its affiliates. All rights reserved.
33
*/
44
/*
55
* Licensed to the Apache Software Foundation (ASF) under one or more
@@ -20,6 +20,7 @@
2020

2121
package com.sun.org.apache.xml.internal.serializer;
2222

23+
import com.sun.org.apache.xerces.internal.util.XMLChar;
2324
import com.sun.org.apache.xml.internal.serializer.dom3.DOMConstants;
2425
import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
2526
import com.sun.org.apache.xml.internal.serializer.utils.Utils;
@@ -54,7 +55,7 @@
5455
* serializers (xml, html, text ...) that write output to a stream.
5556
*
5657
* @xsl.usage internal
57-
* @LastModified: June 2021
58+
* @LastModified: Mar 2022
5859
*/
5960
abstract public class ToStream extends SerializerBase {
6061

@@ -1745,13 +1746,19 @@ protected int accumDefaultEscape(
17451746
}
17461747
else
17471748
{
1748-
/* This if check is added to support control characters in XML 1.1.
1749-
* If a character is a Control Character within C0 and C1 range, it is desirable
1750-
* to write it out as Numeric Character Reference(NCR) regardless of XML Version
1751-
* being used for output document.
1749+
/*
1750+
* The check was added to support control characters in XML 1.1.
1751+
* It previously wrote Control Characters within C0 and C1 range
1752+
* as Numeric Character Reference(NCR) regardless of XML Version,
1753+
* which was incorrect as Control Characters are invalid in XML 1.0.
17521754
*/
1753-
if (isCharacterInC0orC1Range(ch) ||
1754-
(XMLVERSION11.equals(getVersion()) && isNELorLSEPCharacter(ch)))
1755+
boolean isVer11 = XMLVERSION11.equals(getVersion());
1756+
if (!isVer11 && XMLChar.isInvalid(ch)) {
1757+
throw new org.xml.sax.SAXException(Utils.messages.createMessage(
1758+
MsgKey.ER_WF_INVALID_CHARACTER_IN_TEXT,
1759+
new Object[]{Integer.toHexString(ch)}));
1760+
}
1761+
if (isCharacterInC0orC1Range(ch) || (isVer11 && isNELorLSEPCharacter(ch)))
17551762
{
17561763
writeCharRef(writer, ch);
17571764
}

‎test/jaxp/javax/xml/jaxp/unittest/transform/OpenJDK100017Test.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,7 @@
2323

2424
package transform;
2525

26+
import com.sun.org.apache.xerces.internal.util.XMLChar;
2627
import java.io.IOException;
2728

2829
import javax.xml.transform.TransformerConfigurationException;
@@ -32,19 +33,16 @@
3233
import javax.xml.transform.stream.StreamResult;
3334

3435
import org.testng.Assert;
35-
import org.testng.annotations.Listeners;
3636
import org.testng.annotations.Test;
3737
import org.xml.sax.SAXException;
3838

3939
/*
4040
* @test
41-
* @bug 6883209
42-
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
43-
* @run testng/othervm -DrunSecMngr=true -Djava.security.manager=allow transform.OpenJDK100017Test
41+
* @bug 6883209 8273370
42+
* @modules java.xml/com.sun.org.apache.xerces.internal.util
4443
* @run testng/othervm transform.OpenJDK100017Test
4544
* @summary Test XSLT won't cause StackOverflow when it handle many characters.
4645
*/
47-
@Listeners({jaxp.library.BasePolicy.class})
4846
public class OpenJDK100017Test {
4947

5048
@Test
@@ -56,7 +54,9 @@ public final void testXMLStackOverflowBug() throws TransformerConfigurationExcep
5654

5755
StringBuilder sb = new StringBuilder(4096);
5856
for (int x = 4096; x > 0; x--) {
59-
sb.append((char) x);
57+
if (XMLChar.isValid(x)) {
58+
sb.append((char)x);
59+
}
6060
}
6161
ser.characters(sb.toString().toCharArray(), 0, sb.toString().toCharArray().length);
6262
ser.endDocument();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
package transform;
24+
25+
import java.io.BufferedWriter;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.File;
28+
import java.io.FileInputStream;
29+
import java.io.IOException;
30+
import java.io.OutputStream;
31+
import java.io.OutputStreamWriter;
32+
import java.util.prefs.InvalidPreferencesFormatException;
33+
import java.util.prefs.Preferences;
34+
import javax.xml.parsers.DocumentBuilderFactory;
35+
import javax.xml.parsers.ParserConfigurationException;
36+
import javax.xml.transform.OutputKeys;
37+
import javax.xml.transform.Transformer;
38+
import javax.xml.transform.TransformerException;
39+
import javax.xml.transform.TransformerFactory;
40+
import javax.xml.transform.dom.DOMSource;
41+
import javax.xml.transform.stream.StreamResult;
42+
import org.testng.Assert;
43+
import org.testng.annotations.Test;
44+
import org.w3c.dom.DOMImplementation;
45+
import org.w3c.dom.Document;
46+
import org.w3c.dom.DocumentType;
47+
import org.w3c.dom.Element;
48+
49+
/*
50+
* @test
51+
* @bug 8273370
52+
* @run testng transform.SerializationTest
53+
* @summary Verifies that the characters are written correctly during serialization.
54+
*/
55+
public class SerializationTest {
56+
57+
private static final String PREFS_DTD_URI
58+
= "http://java.sun.com/dtd/preferences.dtd";
59+
private static String CLS_DIR = System.getProperty("test.classes", ".");
60+
private static String SRC_DIR = System.getProperty("test.src");
61+
62+
/**
63+
* Verifies that the XMLSupport for exportSubtree handles control characters
64+
* correctly by reporting en error.
65+
*
66+
* Note: exportSubtree currently throws AssertionError. It would be more
67+
* appropriate to throw InvalidPreferencesFormatException as the import
68+
* method does. Since this is an edge case however, we'll keep it as is to
69+
* avoid signature change.
70+
*
71+
* The following was the original test:
72+
Preferences p = Preferences.userRoot().node("test");
73+
p.put("key", "[\u0018\u0019]");
74+
p.exportSubtree(new ByteArrayOutputStream());
75+
*
76+
* The code however, hanged when running in JTReg. This test therefore replaced
77+
* the above code with the process extracted from the exportSubtree routine.
78+
*
79+
* @throws Exception if the test fails
80+
*/
81+
@Test
82+
public void testTrasformer() throws Exception {
83+
Assert.assertThrows(AssertionError.class,
84+
() -> export(new ByteArrayOutputStream()));
85+
}
86+
87+
private void export(OutputStream os) throws IOException {
88+
Document doc = createPrefsDoc("preferences");
89+
Element preferences = doc.getDocumentElement();
90+
preferences.setAttribute("EXTERNAL_XML_VERSION", "1.0");
91+
Element xmlRoot = (Element) preferences.appendChild(doc.createElement("root"));
92+
xmlRoot.setAttribute("type", "user");
93+
94+
Element e = xmlRoot;
95+
96+
e.appendChild(doc.createElement("map"));
97+
e = (Element) e.appendChild(doc.createElement("node"));
98+
e.setAttribute("name", "test");
99+
100+
putPreferencesInXml(e, doc);
101+
102+
writeDoc(doc, os);
103+
}
104+
105+
private static Document createPrefsDoc(String qname) {
106+
try {
107+
DOMImplementation di = DocumentBuilderFactory.newInstance().
108+
newDocumentBuilder().getDOMImplementation();
109+
DocumentType dt = di.createDocumentType(qname, null, PREFS_DTD_URI);
110+
return di.createDocument(null, qname, dt);
111+
} catch (ParserConfigurationException e) {
112+
throw new AssertionError(e);
113+
}
114+
}
115+
116+
private static void putPreferencesInXml(Element elt, Document doc) {
117+
Element map = (Element) elt.appendChild(doc.createElement("map"));
118+
Element entry = (Element) map.appendChild(doc.createElement("entry"));
119+
entry.setAttribute("key", "key");
120+
entry.setAttribute("value", "[\u0018\u0019]");
121+
}
122+
123+
private void writeDoc(Document doc, OutputStream out)
124+
throws IOException {
125+
try {
126+
TransformerFactory tf = TransformerFactory.newInstance();
127+
tf.setAttribute("indent-number", 2);
128+
Transformer t = tf.newTransformer();
129+
t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doc.getDoctype().getSystemId());
130+
t.setOutputProperty(OutputKeys.INDENT, "yes");
131+
//Transformer resets the "indent" info if the "result" is a StreamResult with
132+
//an OutputStream object embedded, creating a Writer object on top of that
133+
//OutputStream object however works.
134+
t.transform(new DOMSource(doc),
135+
new StreamResult(new BufferedWriter(new OutputStreamWriter(out, "UTF-8"))));
136+
} catch (TransformerException e) {
137+
throw new AssertionError(e);
138+
}
139+
}
140+
141+
/**
142+
* Verifies that the XMLSupport for importPreferences handles control
143+
* characters correctly by reporting en error.
144+
*
145+
* Note: this is the existing behavior. This test is here to match with the
146+
* export method.
147+
*
148+
* "preferences.xml" was generated by calling the exportSubtree method
149+
* before the patch.
150+
*
151+
* @throws Exception if the test fails
152+
*/
153+
@Test
154+
public void testParser() throws Exception {
155+
Assert.assertThrows(InvalidPreferencesFormatException.class, () -> {
156+
Preferences.importPreferences(
157+
new FileInputStream(new File(SRC_DIR + "/preferences.xml")));
158+
});
159+
}
160+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
3+
<preferences EXTERNAL_XML_VERSION="1.0">
4+
<root type="user">
5+
<map/>
6+
<node name="test">
7+
<map>
8+
<entry key="key" value="[&#24;&#25;]"/>
9+
</map>
10+
</node>
11+
</root>
12+
</preferences>

0 commit comments

Comments
 (0)
Please sign in to comment.