Merge the 2018-09-05 SPL branch from AOSP-Partner

* security-aosp-nyc-mr2-release:
  Fix hostname parsing in java.net.URLStreamHandler.

Change-Id: I4d26b24446f8857647a6f95103360d413dc81ceb
diff --git a/luni/src/test/java/libcore/java/net/DatagramSocketTest.java b/luni/src/test/java/libcore/java/net/DatagramSocketTest.java
index 067291a..2e091ab 100644
--- a/luni/src/test/java/libcore/java/net/DatagramSocketTest.java
+++ b/luni/src/test/java/libcore/java/net/DatagramSocketTest.java
@@ -19,10 +19,12 @@
 import junit.framework.TestCase;
 
 import java.lang.reflect.Field;
+import java.net.DatagramPacket;
 import java.net.DatagramSocket;
-import java.net.InetSocketAddress;
 import java.net.DatagramSocketImpl;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketException;
 
 public class DatagramSocketTest extends TestCase {
 
@@ -58,6 +60,36 @@
     assertEquals(-1, ds.getLocalPort());
     assertNull(ds.getLocalSocketAddress());
   }
+
+  public void testPendingException() throws Exception {
+    final int port = 9999;
+
+    try (DatagramSocket s = new DatagramSocket()) {
+      s.connect(InetAddress.getLocalHost(), port);
+
+      // connect may set pendingConnectException on internal failure; since we have no reliable way
+      // to make connect fail, set pendingConnectException through reflection.
+      Field pendingConnectException = s.getClass().getDeclaredField("pendingConnectException");
+      pendingConnectException.setAccessible(true);
+      pendingConnectException.set(s, new SocketException());
+
+      byte[] data = new byte[100];
+      DatagramPacket p = new DatagramPacket(data, data.length);
+
+      try {
+        s.send(p);
+        fail();
+      } catch (SocketException expected) {
+      }
+
+      try {
+        s.receive(p);
+        fail();
+      } catch (SocketException expected) {
+      }
+    }
+  }
+
   // Socket should become connected even if impl.connect() failed and threw exception.
   public void test_b31218085() throws Exception {
     final int port = 9999;
diff --git a/ojluni/src/main/java/java/net/DatagramSocket.java b/ojluni/src/main/java/java/net/DatagramSocket.java
index 13b54b0..6565e34 100755
--- a/ojluni/src/main/java/java/net/DatagramSocket.java
+++ b/ojluni/src/main/java/java/net/DatagramSocket.java
@@ -652,6 +652,11 @@
     public void send(DatagramPacket p) throws IOException  {
         InetAddress packetAddress = null;
         synchronized (p) {
+            // ----- BEGIN android -----
+            if (pendingConnectException != null) {
+                throw new SocketException("Pending connect failure", pendingConnectException);
+            }
+            // ----- END android -----
             if (isClosed())
                 throw new SocketException("Socket is closed");
             checkAddress (p.getAddress(), "send");
diff --git a/ojluni/src/main/java/java/net/URI.java b/ojluni/src/main/java/java/net/URI.java
index 5a78d6e..0f8cea1 100755
--- a/ojluni/src/main/java/java/net/URI.java
+++ b/ojluni/src/main/java/java/net/URI.java
@@ -586,6 +586,7 @@
      *          by the above deviations
      */
     public URI(String str) throws URISyntaxException {
+        java.util.SeempLog.record_str(92, "s:"+str);
         new Parser(str).parse(false);
     }
 
diff --git a/ojluni/src/main/java/java/net/URL.java b/ojluni/src/main/java/java/net/URL.java
index 70825a0..803eb7c 100755
--- a/ojluni/src/main/java/java/net/URL.java
+++ b/ojluni/src/main/java/java/net/URL.java
@@ -988,6 +988,7 @@
      *             int, java.lang.String)
      */
     public URLConnection openConnection() throws java.io.IOException {
+        java.util.SeempLog.record_str(91, "URL:"+query);
         return handler.openConnection(this);
     }
 
diff --git a/ojluni/src/main/java/java/net/URLStreamHandler.java b/ojluni/src/main/java/java/net/URLStreamHandler.java
index 49a84a9..8a7e5ae 100755
--- a/ojluni/src/main/java/java/net/URLStreamHandler.java
+++ b/ojluni/src/main/java/java/net/URLStreamHandler.java
@@ -188,6 +188,14 @@
             }
             // END Android-changed: Check for all hostname termination chars. http://b/110955991
 
+            // ----- BEGIN android -----
+            // i may become greater than limit
+            // b/31858037
+            if (i > limit) {
+                i = limit;
+            }
+            // ----- END android -----
+
             host = authority = spec.substring(start, i);
 
             int ind = authority.indexOf('@');
diff --git a/ojluni/src/main/java/java/util/SeempLog.java b/ojluni/src/main/java/java/util/SeempLog.java
new file mode 100644
index 0000000..f8a72d7
--- /dev/null
+++ b/ojluni/src/main/java/java/util/SeempLog.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above
+ *     copyright notice, this list of conditions and the following
+ *     disclaimer in the documentation and/or other materials provided
+ *     with the distribution.
+ *   * Neither the name of The Linux Foundation nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package java.util;
+
+/*
+import com.android.internal.os.RuntimeInit;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.UnknownHostException;
+*/
+
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ *
+ * @hide
+ */
+public final class SeempLog {
+
+    private static Method seemp_record_method = null;
+    private static boolean seemp_record_method_looked_up = false;
+
+    private SeempLog() {
+    }
+
+    /**
+     * Send a log message to the seemp log.
+     * @param msg The message you would like logged.
+     */
+    public static int record_str(int api, String msg) {
+        if (seemp_record_method == null) {
+            if (!seemp_record_method_looked_up) {
+                try
+                {
+                    Class c = Class.forName( "android.util.SeempLog" );
+                    if (c!=null) {
+                        seemp_record_method = c.getDeclaredMethod( "record_str", int.class, String.class );
+                    }
+                } catch (ClassNotFoundException ex) {
+                    seemp_record_method = null;
+                } catch (NoSuchMethodException ex) {
+                    seemp_record_method = null;
+                }
+
+            }
+            seemp_record_method_looked_up = true;
+        }
+
+        if (seemp_record_method!=null) {
+            try {
+                return ( ( Integer )seemp_record_method.invoke( null, api, msg ) ).intValue();
+            } catch  (IllegalAccessException ex) {
+                return 0;
+            } catch (InvocationTargetException ex) {
+                return 0;
+            }
+        }
+
+        return 0;// seemp_println_native("-1|" + msg);
+    }
+
+}
diff --git a/openjdk_java_files.mk b/openjdk_java_files.mk
index d746447..41904a9 100644
--- a/openjdk_java_files.mk
+++ b/openjdk_java_files.mk
@@ -859,6 +859,7 @@
     ojluni/src/main/java/java/util/RegularEnumSet.java \
     ojluni/src/main/java/java/util/ResourceBundle.java \
     ojluni/src/main/java/java/util/Scanner.java \
+    ojluni/src/main/java/java/util/SeempLog.java \
     ojluni/src/main/java/java/util/ServiceConfigurationError.java \
     ojluni/src/main/java/java/util/ServiceLoader.java \
     ojluni/src/main/java/java/util/Set.java \
diff --git a/xml/src/main/java/org/kxml2/io/KXmlParser.java b/xml/src/main/java/org/kxml2/io/KXmlParser.java
index e010f1d..deafa38 100644
--- a/xml/src/main/java/org/kxml2/io/KXmlParser.java
+++ b/xml/src/main/java/org/kxml2/io/KXmlParser.java
@@ -157,6 +157,8 @@
     private boolean isWhitespace;
     private String namespace;
     private String prefix;
+    private String foundPrefix = null;
+    private String foundName = null;
     private String name;
     private String text;
 
@@ -268,15 +270,21 @@
             }
         }
 
-        int cut = name.indexOf(':');
+        if (foundPrefix != null && foundName != null) {
+            prefix = foundPrefix;
+            name = foundName;
+        }
+        else {
+            int cut = name.indexOf(':');
 
         if (cut == 0) {
             checkRelaxed("illegal tag name: " + name);
         }
 
-        if (cut != -1) {
-            prefix = name.substring(0, cut);
-            name = name.substring(cut + 1);
+            if (cut != -1) {
+                prefix = name.substring(0, cut);
+                name = name.substring(cut + 1);
+            }
         }
 
         this.namespace = getNamespace(prefix);
@@ -962,13 +970,19 @@
     }
 
     private void readEndTag() throws IOException, XmlPullParserException {
+        int sp = (depth - 1) * 4;
         read('<');
         read('/');
-        name = readName(); // TODO: pass the expected name in as a hint?
+        if (depth == 0) {
+            name = readName();
+        }
+        else {
+            // Pass the expected name in as a hint.
+            name = readExpectedName(elementStack[sp + 3]);
+        }
         skip();
         read('>');
 
-        int sp = (depth - 1) * 4;
 
         if (depth == 0) {
             checkRelaxed("read end tag " + name + " with no tags open");
@@ -1056,7 +1070,7 @@
         if (!xmldecl) {
             read('<');
         }
-        name = readName();
+        name = readName(true);
         attributeCount = 0;
 
         while (true) {
@@ -1430,7 +1444,12 @@
         }
 
         if (result == null) {
-            return stringPool.get(buffer, start, position - start);
+            if (isWhitespace) {
+                return stringPool.get(buffer, start, position - start);
+            }
+            else {
+                return new String(buffer, start, position - start);
+            }
         } else {
             result.append(buffer, start, position - start);
             return result.toString();
@@ -1526,13 +1545,44 @@
      * Returns an element or attribute name. This is always non-empty for
      * non-relaxed parsers.
      */
+    private String readExpectedName(String expected) throws IOException, XmlPullParserException {
+        int length = expected.length();
+        int end = position + length;
+        if (end < limit) {
+            // Fast path for normal case.
+            boolean match = true;
+            for (int i = 0; i < length; i++) {
+                if (buffer[position+i] != expected.charAt(i)) {
+                    match = false;
+                    break;
+                }
+            }
+            if (match) {
+                position += length;
+                return expected;
+            }
+        }
+        return readName();
+    }
+
     private String readName() throws IOException, XmlPullParserException {
+        return readName(false);
+    }
+
+    /**
+     * Returns an element or attribute name. This is always non-empty for
+     * non-relaxed parsers.  findPrefix should only be true for element names
+     */
+    private String readName(boolean findPrefix) throws IOException, XmlPullParserException {
         if (position >= limit && !fillBuffer(1)) {
             checkRelaxed("name expected");
             return "";
         }
 
         int start = position;
+        int nameStart = -1;
+        foundPrefix = null;
+        foundName = null;
         StringBuilder result = null;
 
         // read the first character
@@ -1576,12 +1626,20 @@
                     || c == ':'
                     || c == '.'
                     || c >= '\u00b7') {  // TODO: check the XML spec
+                // Fast path for common case
+                if (c == ':' && findPrefix && foundPrefix == null) {
+                    foundPrefix = stringPool.get(buffer, start, position - start);
+                    nameStart = position+1;
+                }
                 position++;
                 continue;
             }
 
             // we encountered a non-name character. done!
             if (result == null) {
+                if (foundPrefix != null && position != nameStart) {
+                    foundName = stringPool.get(buffer, nameStart, position - nameStart);
+                }
                 return stringPool.get(buffer, start, position - start);
             } else {
                 result.append(buffer, start, position - start);