[automerger skipped] DO NOT MERGE Use explicitly opened file with O_WRONLY in test_sendfile_errno am: f0324559dd -s ours am: b800078f99 -s ours am: 4c2c16a044 -s ours am: 014b3ea94b -s ours am: 1ee27cb06b -s ours

am skip reason: subject contains skip directive

Original change: https://android-review.googlesource.com/c/platform/libcore/+/2241795

Change-Id: I188ef117cfa09a7314d5568a6682552933eee34e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java b/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java
index f0f79ad..8375421 100644
--- a/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java
+++ b/luni/src/test/java/libcore/java/net/NetworkInterfaceTest.java
@@ -17,6 +17,7 @@
 package libcore.java.net;
 
 import junit.framework.TestCase;
+import org.mockito.Mockito;
 
 import android.system.StructIfaddrs;
 import java.io.BufferedReader;
@@ -39,6 +40,7 @@
 import java.util.regex.Pattern;
 import libcore.io.IoUtils;
 import libcore.io.Libcore;
+import libcore.io.Os;
 
 import static android.system.OsConstants.AF_INET;
 import static android.system.OsConstants.IFF_LOOPBACK;
@@ -48,6 +50,7 @@
 import static android.system.OsConstants.IFF_UP;
 import static android.system.OsConstants.SOCK_DGRAM;
 import static java.net.NetworkInterface.getNetworkInterfaces;
+import static org.mockito.ArgumentMatchers.anyString;
 
 public class NetworkInterfaceTest extends TestCase {
     // http://code.google.com/p/android/issues/detail?id=13784
@@ -221,6 +224,29 @@
         assertEquals(ifaddrsNames, actualNiNames);
     }
 
+    // Validate that we don't fail to enumerate interfaces if there is virtual interface without parent interface present.
+    // b/159277702
+    public void testGetNetworkInterfaces_OrphanInterfaceDoesNotThrow() throws Exception {
+        Os originalOs = Libcore.getOs();
+        Os mockOs = Mockito.mock(Os.class);
+
+        try {
+            Mockito.when(mockOs.getifaddrs()).thenReturn(new StructIfaddrs[] {
+                new StructIfaddrs("dummy0:1", 0, null, null, null, null),
+            });
+
+            Mockito.when(mockOs.if_nametoindex(anyString())).thenReturn(1);
+
+            assertTrue("Failed to swap OS implementation", Libcore.compareAndSetOs(originalOs, mockOs));
+
+            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
+            assertEquals(1, Collections.list(interfaces).size());
+        }
+        finally {
+            assertTrue("Failed to revert OS implementation", Libcore.compareAndSetOs(mockOs, originalOs));
+        }
+    }
+
     // Calling getSubInterfaces on interfaces with no subinterface should not throw NPE.
     // http://b/33844501
     public void testGetSubInterfaces() throws Exception {
diff --git a/ojluni/src/main/java/java/net/NetworkInterface.java b/ojluni/src/main/java/java/net/NetworkInterface.java
index a30b6bf..e56e0cb 100644
--- a/ojluni/src/main/java/java/net/NetworkInterface.java
+++ b/ojluni/src/main/java/java/net/NetworkInterface.java
@@ -494,8 +494,11 @@
                 NetworkInterface parent = nis.get(parentName);
 
                 ni.virtual = true;
-                ni.parent = parent;
-                parent.childs.add(ni);
+
+                if (parent != null) {
+                    ni.parent = parent;
+                    parent.childs.add(ni);
+                }
             }
         }
 
diff --git a/ojluni/src/main/native/zip_util.c b/ojluni/src/main/native/zip_util.c
index 6c02d2b..aa9c5ce 100644
--- a/ojluni/src/main/native/zip_util.c
+++ b/ojluni/src/main/native/zip_util.c
@@ -156,7 +156,7 @@
      * JVM_IO_INTR is tricky and could cause undesired side effect. So we decided
      * to simply call "read" on Solaris/Linux. See details in bug 6304463.
      */
-    return pread(zfd, buf, nbytes, offset);
+    return pread64(zfd, buf, nbytes, offset);
 #endif
 }