1/* 2 * Copyright 2014 Google, Inc. 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 24package parse.jdk7156085; 25 26import java.io.ByteArrayInputStream; 27import java.io.IOException; 28import javax.xml.parsers.SAXParser; 29import javax.xml.parsers.SAXParserFactory; 30import org.xml.sax.InputSource; 31import org.xml.sax.helpers.DefaultHandler; 32import org.testng.annotations.Test; 33 34/** 35 * JDK-7156085: ArrayIndexOutOfBoundsException throws in UTF8Reader of SAXParser 36 * https://bugs.openjdk.java.net/browse/JDK-7156085 37 * 38 * XERCESJ-1257: buffer overflow in UTF8Reader for characters out of BMP 39 * https://issues.apache.org/jira/browse/XERCESJ-1257 40 */ 41public class UTF8ReaderBug { 42 @Test 43 public void shouldAcceptSupplementaryCharacters() throws Throwable { 44 StringBuilder b = new StringBuilder("<xml>"); 45 for(int i = 5; i < 8223; i++) { 46 b.append(' '); 47 } 48 // Add surrogate characters which overflow the buffer. This shows the need to place an 49 // overflow check at -- 50 // com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:544) 51 b.append("\uD835\uDC37"); 52 b.append("</xml>"); 53 sendToParser(b.toString()); 54 } 55 56 private static void sendToParser(String b) throws Throwable { 57 byte[] input = b.getBytes("UTF-8"); 58 ByteArrayInputStream in = new ByteArrayInputStream(input); 59 60 SAXParserFactory spf = SAXParserFactory.newInstance(); 61 SAXParser p = spf.newSAXParser(); 62 p.parse(new InputSource(in), new DefaultHandler()); 63 } 64} 65