8202426: NPE thrown by Transformer when XMLStreamReader reports no xml attribute type

Reviewed-by: lancea
This commit is contained in:
Joe Wang 2018-05-15 13:28:08 -07:00
parent a060be188d
commit e8586cd4bc
5 changed files with 197 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@ -22,6 +22,7 @@
package com.sun.org.apache.xalan.internal.xsltc.trax;
import com.sun.org.apache.xalan.internal.xsltc.runtime.Constants;
import com.sun.org.apache.xerces.internal.util.XMLSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
@ -194,14 +195,16 @@ public class SAX2DOM implements ContentHandler, LexicalHandler, Constants {
// checking if Namespace processing is being done
String attQName = attrs.getQName(i);
String attURI = attrs.getURI(i);
String type = (attrs.getType(i) == null) ?
XMLSymbols.fCDATASymbol : attrs.getType(i);
if (attrs.getLocalName(i).equals("")) {
tmp.setAttribute(attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
if (type.equals("ID")) {
tmp.setIdAttribute(attQName, true);
}
} else {
tmp.setAttributeNS(attURI, attQName, attrs.getValue(i));
if (attrs.getType(i).equals("ID")) {
if (type.equals("ID")) {
tmp.setIdAttributeNS(attURI, attrs.getLocalName(i), true);
}
}

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jaxp.library;
import static jaxp.library.JAXPTestUtilities.getSystemProperty;
import java.io.FilePermission;
import java.net.SocketPermission;
import org.testng.ITestContext;
/**
* Covers all policies currently required for running JAXP tests
*/
public class JAXPTestPolicy extends BasePolicy {
@Override
public void onStart(ITestContext arg0) {
if (isRunWithSecurityManager()) {
JAXPPolicyManager policyManager = JAXPPolicyManager.getJAXPPolicyManager(true);
String userdir = getSystemProperty("user.dir");
policyManager.addPermission(new FilePermission(userdir + "/-", "read,write,delete"));
String testSrc = System.getProperty("test.src");
// to handle the directory structure of some functional test suite
if (testSrc.endsWith("ptests"))
testSrc = testSrc.substring(0, testSrc.length() - 7);
policyManager.addPermission(new FilePermission(testSrc + "/-", "read"));
policyManager.addPermission(new FilePermission(userdir, "read"));
policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xerces.internal.jaxp"));
policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xerces.internal.impl"));
policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xerces.internal.xni.parser"));
policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.bcel.internal.classfile"));
policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.bcel.internal.generic"));
policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.org.apache.xalan.internal.xsltc.trax"));
policyManager.addPermission(new RuntimePermission("accessClassInPackage.com.sun.xml.internal.stream"));
policyManager.addPermission(new SocketPermission("openjdk.java.net:80", "connect,resolve"));
policyManager.addPermission(new SocketPermission("www.w3.org:80", "connect,resolve"));
}
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package transform;
import com.sun.org.apache.xerces.internal.impl.PropertyManager;
import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
import com.sun.xml.internal.stream.XMLInputFactoryImpl;
import java.io.Reader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
public class MyXMLInputFactoryImpl extends XMLInputFactoryImpl {
//List of supported properties and default values.
private PropertyManager fPropertyManager = new PropertyManager(PropertyManager.CONTEXT_READER);
public MyXMLInputFactoryImpl() {
super();
}
public XMLStreamReader createXMLStreamReader(Reader reader) throws XMLStreamException {
XMLInputSource inputSource = new XMLInputSource(null, null, null, reader, null);
return getXMLStreamReaderImpl(inputSource);
}
XMLStreamReader getXMLStreamReaderImpl(XMLInputSource inputSource) throws javax.xml.stream.XMLStreamException {
return new MyXMLStreamReader(inputSource, new PropertyManager(fPropertyManager));
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package transform;
import com.sun.org.apache.xerces.internal.impl.PropertyManager;
import com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl;
import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
import java.io.InputStream;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
public class MyXMLStreamReader extends XMLStreamReaderImpl implements XMLStreamReader {
public MyXMLStreamReader(InputStream inputStream, PropertyManager props) throws XMLStreamException {
super(inputStream, props);
}
public MyXMLStreamReader(XMLInputSource inputSource, PropertyManager props)
throws XMLStreamException {
super(inputSource, props);
}
public String getAttributeType(int index) {
return null;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,9 +23,9 @@
package transform;
import static jaxp.library.JAXPTestUtilities.setSystemProperty;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
@ -48,14 +48,44 @@ import org.testng.annotations.Test;
/*
* @test
* @bug 8152530
* @bug 8152530 8202426
* @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
* @modules java.xml
* @modules java.xml/com.sun.org.apache.xerces.internal.impl
* @modules java.xml/com.sun.org.apache.xerces.internal.xni.parser
* @modules java.xml/com.sun.xml.internal.stream
* @clean MyXMLInputFactoryImpl MyXMLStreamReader
* @build MyXMLInputFactoryImpl MyXMLStreamReader
* @run testng/othervm -DrunSecMngr=true transform.StAXSourceTest
* @run testng/othervm transform.StAXSourceTest
* @summary Test parsing from StAXSource.
*/
@Listeners({jaxp.library.FilePolicy.class})
@Listeners({jaxp.library.JAXPTestPolicy.class})
public class StAXSourceTest {
/**
* @bug 8202426
* Verifies that a null Attribute type is handled. NPE was thrown before the fix.
*/
@Test
public final void testAttributeTypeNull() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" +
"<t:test xmlns:t=\"http://www.example.org/Test\" attr=\"value\" /> ";
setSystemProperty("javax.xml.stream.XMLInputFactory", "transform.MyXMLInputFactoryImpl");
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xml));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while (xsr.hasNext()) {
xsr.next();
if (xsr.getEventType() == XMLStreamConstants.START_ELEMENT) {
t.reset();
DOMResult result = new DOMResult();
t.transform(new StAXSource(xsr), result);
}
}
}
/**
* @bug 8152530
* Verifies that StAXSource handles empty namespace properly. NPE was thrown