Path: blob/master/test/jdk/javax/xml/jaxp/testng/parse/jdk7156085/UTF8ReaderBug.java
41161 views
/*1* Copyright 2014 Google, Inc. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package parse.jdk7156085;2425import java.io.ByteArrayInputStream;26import java.io.IOException;27import javax.xml.parsers.SAXParser;28import javax.xml.parsers.SAXParserFactory;29import org.xml.sax.InputSource;30import org.xml.sax.helpers.DefaultHandler;31import org.testng.annotations.Test;3233/**34* JDK-7156085: ArrayIndexOutOfBoundsException throws in UTF8Reader of SAXParser35* https://bugs.openjdk.java.net/browse/JDK-715608536*37* XERCESJ-1257: buffer overflow in UTF8Reader for characters out of BMP38* https://issues.apache.org/jira/browse/XERCESJ-125739*/40public class UTF8ReaderBug {41@Test42public void shouldAcceptSupplementaryCharacters() throws Throwable {43StringBuilder b = new StringBuilder("<xml>");44for(int i = 5; i < 8223; i++) {45b.append(' ');46}47// Add surrogate characters which overflow the buffer. This shows the need to place an48// overflow check at --49// com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:544)50b.append("\uD835\uDC37");51b.append("</xml>");52sendToParser(b.toString());53}5455private static void sendToParser(String b) throws Throwable {56byte[] input = b.getBytes("UTF-8");57ByteArrayInputStream in = new ByteArrayInputStream(input);5859SAXParserFactory spf = SAXParserFactory.newInstance();60SAXParser p = spf.newSAXParser();61p.parse(new InputSource(in), new DefaultHandler());62}63}646566