Skip to content

Commit 4e57177

Browse files
committedSep 21, 2023
8316383: NullPointerException in AbstractSAXParser after JDK-8306632
Reviewed-by: lancea, naoto
1 parent 3b397c8 commit 4e57177

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed
 

‎src/java.xml/share/classes/com/sun/org/apache/xerces/internal/parsers/SAXParser.java

+26-11
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
2828
import jdk.xml.internal.JdkConstants;
2929
import jdk.xml.internal.JdkProperty;
30+
import jdk.xml.internal.Utils;
3031
import jdk.xml.internal.XMLSecurityManager;
32+
import org.xml.sax.SAXException;
3133
import org.xml.sax.SAXNotRecognizedException;
3234
import org.xml.sax.SAXNotSupportedException;
3335

@@ -39,7 +41,7 @@
3941
* @author Arnaud Le Hors, IBM
4042
* @author Andy Clark, IBM
4143
*
42-
* @LastModified: July 2023
44+
* @LastModified: Sep 2023
4345
*/
4446
public class SAXParser
4547
extends AbstractSAXParser {
@@ -89,6 +91,7 @@ public class SAXParser
8991
*/
9092
public SAXParser(XMLParserConfiguration config) {
9193
super(config);
94+
initSecurityManager();
9295
} // <init>(XMLParserConfiguration)
9396

9497
/**
@@ -125,6 +128,7 @@ public SAXParser(SymbolTable symbolTable, XMLGrammarPool grammarPool) {
125128
fConfiguration.setProperty(XMLGRAMMAR_POOL, grammarPool);
126129
}
127130

131+
initSecurityManager();
128132
} // <init>(SymbolTable,XMLGrammarPool)
129133

130134
/**
@@ -152,16 +156,6 @@ public void setProperty(String name, Object value)
152156
return;
153157
}
154158

155-
if (securityManager == null) {
156-
securityManager = new XMLSecurityManager(true);
157-
super.setProperty(Constants.SECURITY_MANAGER, securityManager);
158-
}
159-
160-
if (securityPropertyManager == null) {
161-
securityPropertyManager = new XMLSecurityPropertyManager();
162-
super.setProperty(JdkConstants.XML_SECURITY_PROPERTY_MANAGER, securityPropertyManager);
163-
}
164-
165159
int index = securityPropertyManager.getIndex(name);
166160
if (index > -1) {
167161
/**
@@ -178,4 +172,25 @@ public void setProperty(String name, Object value)
178172
}
179173
}
180174
}
175+
176+
/**
177+
* Initiates the SecurityManager. This becomes necessary when the SAXParser
178+
* is constructed directly by, for example, XMLReaderFactory rather than
179+
* through SAXParserFactory.
180+
*/
181+
private void initSecurityManager() {
182+
try {
183+
if (securityManager == null) {
184+
securityManager = new XMLSecurityManager(true);
185+
super.setProperty(Constants.SECURITY_MANAGER, securityManager);
186+
}
187+
188+
if (securityPropertyManager == null) {
189+
securityPropertyManager = new XMLSecurityPropertyManager();
190+
super.setProperty(JdkConstants.XML_SECURITY_PROPERTY_MANAGER, securityPropertyManager);
191+
}
192+
} catch (SAXException e) {
193+
Utils.dPrint(() -> e.getMessage());
194+
}
195+
}
181196
} // class SAXParser

‎src/java.xml/share/classes/jdk/xml/internal/Utils.java

+27
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,38 @@
2626
package jdk.xml.internal;
2727

2828
import java.util.Arrays;
29+
import java.util.function.Supplier;
2930

3031
/**
3132
* General utility. Use JdkXmlUtils for XML processing related functions.
3233
*/
3334
public class Utils {
35+
// The debug flag
36+
private static boolean debug = false;
37+
38+
/*
39+
* The {@systemProperty jaxp.debug} property is supported by JAXP factories
40+
* and used to print out information related to the configuration of factories
41+
* and processors
42+
*/
43+
static {
44+
try {
45+
String val = SecuritySupport.getSystemProperty("jaxp.debug");
46+
// Allow simply setting the prop to turn on debug
47+
debug = val != null && !"false".equals(val);
48+
}
49+
catch (SecurityException se) {
50+
debug = false;
51+
}
52+
}
53+
54+
// print out debug information if jaxp.debug is enabled
55+
public static void dPrint(Supplier<String> msgGen) {
56+
if (debug) {
57+
System.err.println("JAXP: " + msgGen.get());
58+
}
59+
}
60+
3461
/**
3562
* Creates a new array with copies of the original array and additional items
3663
* appended to the end of it.

‎test/jaxp/javax/xml/jaxp/unittest/sax/XMLReaderTest.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, 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
@@ -33,11 +33,13 @@
3333
import org.testng.annotations.Listeners;
3434
import org.testng.annotations.Test;
3535
import org.xml.sax.SAXException;
36+
import org.xml.sax.XMLReader;
3637
import org.xml.sax.helpers.XMLReaderAdapter;
38+
import org.xml.sax.helpers.XMLReaderFactory;
3739

3840
/*
3941
* @test
40-
* @bug 8158246
42+
* @bug 8158246 8316383
4143
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
4244
* @run testng/othervm -DrunSecMngr=true -Djava.security.manager=allow sax.XMLReaderTest
4345
* @run testng/othervm sax.XMLReaderTest
@@ -69,4 +71,15 @@ public void testcreateXMLReader() throws SAXException, ParserConfigurationExcept
6971
setSystemProperty(SAX_PROPNAME, className + "nosuch");
7072
XMLReaderAdapter adapter = new XMLReaderAdapter();
7173
}
74+
75+
/*
76+
* @bug 8316383
77+
* Verifies that the XMLReader is initialized properly when it's created
78+
* with XMLReaderFactory.
79+
*/
80+
@Test
81+
public void testCreateXMLReaderWithXMLReaderFactory() throws SAXException, ParserConfigurationException {
82+
XMLReader reader = XMLReaderFactory.createXMLReader();
83+
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
84+
}
7285
}

0 commit comments

Comments
 (0)
Please sign in to comment.