Skip to content

Commit 8606921

Browse files
committed
8354774: DocumentBuilderFactory getAttribute throws NPE
1 parent 84458ec commit 8606921

File tree

11 files changed

+269
-116
lines changed

11 files changed

+269
-116
lines changed

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -371,17 +371,10 @@ else if (name.equals(ENABLE_INLINING)) {
371371
return _cdataChunkSize;
372372
}
373373

374-
/** Check to see if the property is managed by the security manager **/
375-
String propertyValue = (_xmlSecurityManager != null) ?
376-
_xmlSecurityManager.getLimitAsString(name) : null;
377-
if (propertyValue != null) {
378-
return propertyValue;
379-
} else {
380-
propertyValue = (_xmlSecurityPropertyMgr != null) ?
381-
_xmlSecurityPropertyMgr.getValue(name) : null;
382-
if (propertyValue != null) {
383-
return propertyValue;
384-
}
374+
//check if the property is managed by security manager
375+
String value;
376+
if ((value = JdkXmlUtils.getProperty(_xmlSecurityManager, _xmlSecurityPropertyMgr, name)) != null) {
377+
return value;
385378
}
386379

387380
// Throw an exception for all other attributes

src/java.xml/share/classes/com/sun/org/apache/xerces/internal/impl/PropertyManager.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,13 @@ public Object getProperty(String property) {
188188
if (XMLInputFactory.SUPPORT_DTD.equals(property)) {
189189
return fSecurityManager.is(XMLSecurityManager.Limit.STAX_SUPPORT_DTD);
190190
}
191-
/**
192-
* Check to see if the property is managed by the security manager *
193-
*/
194-
String propertyValue = (fSecurityManager != null)
195-
? fSecurityManager.getLimitAsString(property) : null;
196-
/**
197-
* Check to see if the property is managed by the security property
198-
* manager
199-
*/
200-
if (propertyValue == null) {
201-
propertyValue = (fSecurityPropertyMgr != null)
202-
? fSecurityPropertyMgr.getValue(property) : null;
191+
192+
//check if the property is managed by security manager
193+
String value;
194+
if ((value = JdkXmlUtils.getProperty(fSecurityManager, fSecurityPropertyMgr, property)) != null) {
195+
return value;
203196
}
204-
return propertyValue != null ? propertyValue : supportedProps.get(property);
197+
return supportedProps.get(property);
205198
}
206199

207200
/**
@@ -250,15 +243,9 @@ public void setProperty(String property, Object value) {
250243
return;
251244
}
252245

253-
//check if the property is managed by security manager
254-
if (fSecurityManager == null
255-
|| !fSecurityManager.setLimit(property, JdkProperty.State.APIPROPERTY, value)) {
256-
//check if the property is managed by security property manager
257-
if (fSecurityPropertyMgr == null
258-
|| !fSecurityPropertyMgr.setValue(property, FeaturePropertyBase.State.APIPROPERTY, value)) {
259-
//fall back to the existing property manager
260-
supportedProps.put(property, value);
261-
}
246+
if (!JdkXmlUtils.setProperty(fSecurityManager, fSecurityPropertyMgr, property, value)) {
247+
//fall back to the existing property manager
248+
supportedProps.put(property, value);
262249
}
263250

264251
if (equivalentProperty != null) {

src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/DocumentBuilderFactoryImpl.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import javax.xml.parsers.DocumentBuilderFactory;
3030
import javax.xml.parsers.ParserConfigurationException;
3131
import javax.xml.validation.Schema;
32-
import jdk.xml.internal.JdkProperty;
32+
import jdk.xml.internal.JdkXmlUtils;
3333
import jdk.xml.internal.XMLSecurityManager;
3434
import jdk.xml.internal.XMLSecurityPropertyManager;
3535
import org.xml.sax.SAXException;
@@ -114,20 +114,13 @@ public void setAttribute(String name, Object value)
114114
attributes = new HashMap<>();
115115
}
116116

117-
//check if the property is managed by security manager
118-
String pName;
119-
if ((pName = fSecurityManager.find(name)) != null) {
120-
// as the qName is deprecated, let the manager decide whether the
121-
// value shall be changed
122-
fSecurityManager.setLimit(name, JdkProperty.State.APIPROPERTY, value);
123-
attributes.put(pName, fSecurityManager.getLimitAsString(pName));
117+
if (JdkXmlUtils.setProperty(fSecurityManager, fSecurityPropertyMgr, name, value)) {
118+
// necessary as DocumentBuilder recreate property manager
119+
// remove this line once that's changed
120+
attributes.put(name, value);
124121
// no need to create a DocumentBuilderImpl
125122
return;
126-
} else if ((pName = fSecurityPropertyMgr.find(name)) != null) {
127-
attributes.put(pName, value);
128-
return;
129123
}
130-
131124
attributes.put(name, value);
132125

133126
// Test the attribute name by possibly throwing an exception
@@ -146,13 +139,10 @@ public void setAttribute(String name, Object value)
146139
public Object getAttribute(String name)
147140
throws IllegalArgumentException
148141
{
149-
150142
//check if the property is managed by security manager
151-
String pName;
152-
if ((pName = fSecurityManager.find(name)) != null) {
153-
return fSecurityManager.getLimitAsString(pName);
154-
} else if ((pName = fSecurityPropertyMgr.find(name)) != null) {
155-
return attributes.get(pName);
143+
String value;
144+
if ((value = JdkXmlUtils.getProperty(fSecurityManager, fSecurityPropertyMgr, name)) != null) {
145+
return value;
156146
}
157147

158148
// See if it's in the attributes Map

src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/DocumentBuilderImpl.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
4444
import jdk.xml.internal.FeaturePropertyBase.State;
4545
import jdk.xml.internal.JdkConstants;
46-
import jdk.xml.internal.JdkProperty;
46+
import jdk.xml.internal.JdkXmlUtils;
4747
import jdk.xml.internal.XMLSecurityManager;
4848
import jdk.xml.internal.XMLSecurityPropertyManager;
4949
import jdk.xml.internal.XMLSecurityPropertyManager.Property;
@@ -297,17 +297,10 @@ private void setDocumentBuilderFactoryAttributes( Map<String, Object> dbfAttrs)
297297
}
298298
}
299299
} else {
300-
//check if the property is managed by security manager
301-
if (fSecurityManager == null ||
302-
!fSecurityManager.setLimit(name, JdkProperty.State.APIPROPERTY, val)) {
303-
//check if the property is managed by security property manager
304-
if (fSecurityPropertyMgr == null ||
305-
!fSecurityPropertyMgr.setValue(name, State.APIPROPERTY, val)) {
306-
//fall back to the existing property manager
307-
domParser.setProperty(name, val);
308-
}
300+
if (!JdkXmlUtils.setProperty(fSecurityManager, fSecurityPropertyMgr, name, val)) {
301+
//fall back to the existing property manager
302+
domParser.setProperty(name, val);
309303
}
310-
311304
}
312305
}
313306
}

src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/SAXParserImpl.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import javax.xml.validation.Schema;
4444
import jdk.xml.internal.FeaturePropertyBase;
4545
import jdk.xml.internal.JdkConstants;
46-
import jdk.xml.internal.JdkProperty;
46+
import jdk.xml.internal.JdkXmlUtils;
4747
import jdk.xml.internal.XMLSecurityManager;
4848
import jdk.xml.internal.XMLSecurityPropertyManager;
4949
import org.xml.sax.EntityResolver;
@@ -569,20 +569,13 @@ else if (JAXP_SCHEMA_SOURCE.equals(name)) {
569569
super.setProperty(name, value);
570570
}
571571

572-
//check if the property is managed by security manager
573-
if (fSecurityManager == null ||
574-
!fSecurityManager.setLimit(name, JdkProperty.State.APIPROPERTY, value)) {
575-
//check if the property is managed by security property manager
576-
if (fSecurityPropertyMgr == null ||
577-
!fSecurityPropertyMgr.setValue(name, FeaturePropertyBase.State.APIPROPERTY, value)) {
578-
//fall back to the existing property manager
579-
if (!fInitProperties.containsKey(name)) {
580-
fInitProperties.put(name, super.getProperty(name));
581-
}
582-
super.setProperty(name, value);
572+
if (!JdkXmlUtils.setProperty(fSecurityManager, fSecurityPropertyMgr, name, value)) {
573+
//fall back to the existing property manager
574+
if (!fInitProperties.containsKey(name)) {
575+
fInitProperties.put(name, super.getProperty(name));
583576
}
577+
super.setProperty(name, value);
584578
}
585-
586579
}
587580

588581
public synchronized Object getProperty(String name)
@@ -596,19 +589,10 @@ public synchronized Object getProperty(String name)
596589
return fSAXParser.schemaLanguage;
597590
}
598591

599-
/** Check to see if the property is managed by the security manager **/
600-
String propertyValue = (fSecurityManager != null) ?
601-
fSecurityManager.getLimitAsString(name) : null;
602-
if (propertyValue != null) {
603-
return propertyValue;
604-
} else {
605-
propertyValue = (fSecurityPropertyMgr != null) ?
606-
fSecurityPropertyMgr.getValue(name) : null;
607-
if (propertyValue != null) {
608-
return propertyValue;
609-
}
592+
String value;
593+
if ((value = JdkXmlUtils.getProperty(fSecurityManager, fSecurityPropertyMgr, name)) != null) {
594+
return value;
610595
}
611-
612596
return super.getProperty(name);
613597
}
614598

src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/validation/XMLSchemaFactory.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,11 @@ else if (name.equals(XMLGRAMMAR_POOL)) {
404404
}
405405
try {
406406
/** Check to see if the property is managed by the security manager **/
407-
String propertyValue = (fSecurityManager != null) ?
408-
fSecurityManager.getLimitAsString(name) : null;
409-
return propertyValue != null ? propertyValue :
410-
fXMLSchemaLoader.getProperty(name);
407+
String value;
408+
if ((value = JdkXmlUtils.getProperty(fSecurityManager, fSecurityPropertyMgr, name)) != null) {
409+
return value;
410+
}
411+
return fXMLSchemaLoader.getProperty(name);
411412
}
412413
catch (XMLConfigurationException e) {
413414
String identifier = e.getIdentifier();
@@ -513,15 +514,9 @@ else if (name.equals(XMLGRAMMAR_POOL)) {
513514
"property-not-supported", new Object [] {name}));
514515
}
515516
try {
516-
//check if the property is managed by security manager
517-
if (fSecurityManager == null ||
518-
!fSecurityManager.setLimit(name, JdkProperty.State.APIPROPERTY, object)) {
519-
//check if the property is managed by security property manager
520-
if (fSecurityPropertyMgr == null ||
521-
!fSecurityPropertyMgr.setValue(name, FeaturePropertyBase.State.APIPROPERTY, object)) {
522-
//fall back to the existing property manager
523-
fXMLSchemaLoader.setProperty(name, object);
524-
}
517+
if (!JdkXmlUtils.setProperty(fSecurityManager, fSecurityPropertyMgr, name, object)) {
518+
//fall back to the existing property manager
519+
fXMLSchemaLoader.setProperty(name, object);
525520
}
526521
}
527522
catch (XMLConfigurationException e) {

src/java.xml/share/classes/com/sun/org/apache/xerces/internal/jaxp/validation/XMLSchemaValidatorComponentManager.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import javax.xml.catalog.CatalogFeatures;
4949
import jdk.xml.internal.FeaturePropertyBase;
5050
import jdk.xml.internal.JdkConstants;
51-
import jdk.xml.internal.JdkProperty;
51+
import jdk.xml.internal.JdkXmlUtils;
5252
import jdk.xml.internal.XMLSecurityManager;
5353
import jdk.xml.internal.XMLSecurityPropertyManager;
5454
import org.w3c.dom.ls.LSResourceResolver;
@@ -353,15 +353,15 @@ else if (SCHEMA_ELEMENT_DEFAULT.equals(featureId)) {
353353
* Set the state of a feature.
354354
*
355355
* @param featureId The unique identifier (URI) of the feature.
356-
* @param state The requested state of the feature (true or false).
356+
* @param value The value of the feature (true or false).
357357
*
358358
* @exception XMLConfigurationException If the requested feature is not known.
359359
*/
360360
public void setFeature(String featureId, boolean value) throws XMLConfigurationException {
361361
if (PARSER_SETTINGS.equals(featureId)) {
362362
throw new XMLConfigurationException(Status.NOT_SUPPORTED, featureId);
363363
}
364-
else if (value == false && (VALIDATION.equals(featureId) || SCHEMA_VALIDATION.equals(featureId))) {
364+
else if (!value && (VALIDATION.equals(featureId) || SCHEMA_VALIDATION.equals(featureId))) {
365365
throw new XMLConfigurationException(Status.NOT_SUPPORTED, featureId);
366366
}
367367
else if (USE_GRAMMAR_POOL_ONLY.equals(featureId) && value != fUseGrammarPoolOnly) {
@@ -452,18 +452,12 @@ else if (LOCALE.equals(propertyId)) {
452452
fComponents.put(propertyId, value);
453453
return;
454454
}
455-
//check if the property is managed by security manager
456-
if (fInitSecurityManager == null ||
457-
!fInitSecurityManager.setLimit(propertyId, JdkProperty.State.APIPROPERTY, value)) {
458-
//check if the property is managed by security property manager
459-
if (fSecurityPropertyMgr == null ||
460-
!fSecurityPropertyMgr.setValue(propertyId, FeaturePropertyBase.State.APIPROPERTY, value)) {
461-
//fall back to the existing property manager
462-
if (!fInitProperties.containsKey(propertyId)) {
463-
fInitProperties.put(propertyId, super.getProperty(propertyId));
464-
}
465-
super.setProperty(propertyId, value);
455+
if (!JdkXmlUtils.setProperty(fInitSecurityManager, fSecurityPropertyMgr, propertyId, value)) {
456+
//fall back to the existing property manager
457+
if (!fInitProperties.containsKey(propertyId)) {
458+
fInitProperties.put(propertyId, super.getProperty(propertyId));
466459
}
460+
super.setProperty(propertyId, value);
467461
}
468462
}
469463

src/java.xml/share/classes/jdk/xml/internal/JdkXmlUtils.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2025, 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
@@ -98,6 +98,48 @@ private static class DefaultSAXFactory {
9898
private static final SAXParserFactory instance = getSAXFactory(false);
9999
}
100100

101+
/**
102+
* Sets the property if it's managed by either XMLSecurityManager or XMLSecurityPropertyManager.
103+
* @param xsm the XMLSecurityManager
104+
* @param xspm the XMLSecurityPropertyManager
105+
* @param property the property
106+
* @param value the value
107+
* @return true if the property is managed by either XMLSecurityManager or
108+
* XMLSecurityPropertyManager, false otherwise
109+
*/
110+
public static boolean setProperty(XMLSecurityManager xsm, XMLSecurityPropertyManager xspm,
111+
String property, Object value) {
112+
String pName;
113+
if (xsm != null && (pName = xsm.find(property)) != null) {
114+
return xsm.setLimit(property, JdkProperty.State.APIPROPERTY, value);
115+
116+
} else if (xspm != null && (pName = xspm.find(property)) != null) {
117+
return xspm.setValue(property, FeaturePropertyBase.State.APIPROPERTY, value);
118+
}
119+
return false;
120+
}
121+
122+
/**
123+
* Returns the value of the property if it's managed by either XMLSecurityManager
124+
* or XMLSecurityPropertyManager.
125+
* @param xsm the XMLSecurityManager
126+
* @param xspm the XMLSecurityPropertyManager
127+
* @param property the property
128+
* @return the value of the property if it's managed by either XMLSecurityManager
129+
* or XMLSecurityPropertyManager, null otherwise
130+
*/
131+
public static String getProperty(XMLSecurityManager xsm, XMLSecurityPropertyManager xspm,
132+
String property) {
133+
String value = null;
134+
if (xsm != null && (value = xsm.getLimitAsString(property)) != null) {
135+
return value;
136+
}
137+
if (xspm != null) {
138+
value = xspm.getValue(property);
139+
}
140+
return value;
141+
}
142+
101143
/**
102144
* Returns the value.
103145
*

src/java.xml/share/classes/jdk/xml/internal/XMLSecurityManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public String find(String propertyName) {
361361
for (Limit limit : Limit.values()) {
362362
if (limit.is(propertyName)) {
363363
// current spec: new property name == systemProperty
364-
return limit.systemProperty();
364+
return (limit.systemProperty != null) ? limit.systemProperty : limit.apiProperty;
365365
}
366366
}
367367
//ENTITYCOUNT's new name is qName

test/jaxp/javax/xml/jaxp/libs/jaxp/library/JUnitTestUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ public class JUnitTestUtil {
3232
public static final String CLS_DIR = System.getProperty("test.classes");
3333
public static final String SRC_DIR = System.getProperty("test.src");
3434

35+
// as in the Processors table in java.xml module summary
36+
public enum Processor {
37+
DOM,
38+
SAX,
39+
XMLREADER,
40+
StAX,
41+
VALIDATION,
42+
TRANSFORM,
43+
XSLTC,
44+
DOMLS,
45+
XPATH
46+
};
47+
3548
/**
3649
* Returns the System identifier (URI) of the source.
3750
* @param path the path to the source

0 commit comments

Comments
 (0)