Fix a bug, and protect against an unrelated class of bugs.

If the Java array allocation in InetAddress.cpp failed, we'd free NULL instead of the
previously-allocated structure. This is a new bug in froyo, but only happens in out of
memory situations, so doesn't seem worth fixing there.

Unrelatedly, let's disallow assignment and copying of all our RAII classes. This isn't
a mistake I've seen made, but it's easy to protect against, so we may as well do so
consistently.

Change-Id: I2433b31ff983d388788b09e59e08d661f1725ecd
diff --git a/libcore/icu/src/main/native/Resources.cpp b/libcore/icu/src/main/native/Resources.cpp
index 9dbdb3d..1dd4bff 100644
--- a/libcore/icu/src/main/native/Resources.cpp
+++ b/libcore/icu/src/main/native/Resources.cpp
@@ -58,6 +58,10 @@
 
 private:
     UResourceBundle* mBundle;
+
+    // Disallow copy and assignment.
+    ScopedResourceBundle(const ScopedResourceBundle&);
+    void operator=(const ScopedResourceBundle&);
 };
 
 static Locale getLocale(JNIEnv* env, jstring locale) {
diff --git a/libcore/icu/src/main/native/ScopedJavaUnicodeString.h b/libcore/icu/src/main/native/ScopedJavaUnicodeString.h
index 44952b4..69726fb 100644
--- a/libcore/icu/src/main/native/ScopedJavaUnicodeString.h
+++ b/libcore/icu/src/main/native/ScopedJavaUnicodeString.h
@@ -46,6 +46,10 @@
     jstring mString;
     const UChar* mChars;
     UnicodeString mUnicodeString;
+
+    // Disallow copy and assignment.
+    ScopedJavaUnicodeString(const ScopedJavaUnicodeString&);
+    void operator=(const ScopedJavaUnicodeString&);
 };
 
 #endif  // SCOPED_JAVA_UNICODE_STRING_H_included
diff --git a/libcore/include/LocalArray.h b/libcore/include/LocalArray.h
index 74c9085..2ab708a 100644
--- a/libcore/include/LocalArray.h
+++ b/libcore/include/LocalArray.h
@@ -66,6 +66,10 @@
     char mOnStackBuffer[STACK_BYTE_COUNT];
     char* mPtr;
     size_t mSize;
+
+    // Disallow copy and assignment.
+    LocalArray(const LocalArray&);
+    void operator=(const LocalArray&);
 };
 
 #endif // LOCAL_ARRAY_H_included
diff --git a/libcore/include/ScopedByteArray.h b/libcore/include/ScopedByteArray.h
index bcbee99..6955b70 100644
--- a/libcore/include/ScopedByteArray.h
+++ b/libcore/include/ScopedByteArray.h
@@ -48,6 +48,10 @@
     JNIEnv* mEnv;
     jbyteArray mByteArray;
     jbyte* mBytes;
+
+    // Disallow copy and assignment.
+    ScopedByteArray(const ScopedByteArray&);
+    void operator=(const ScopedByteArray&);
 };
 
 #endif  // SCOPED_BYTE_ARRAY_H_included
diff --git a/libcore/include/ScopedFd.h b/libcore/include/ScopedFd.h
index 30feabd..d2b7935 100644
--- a/libcore/include/ScopedFd.h
+++ b/libcore/include/ScopedFd.h
@@ -37,6 +37,10 @@
 
 private:
     int fd;
+
+    // Disallow copy and assignment.
+    ScopedFd(const ScopedFd&);
+    void operator=(const ScopedFd&);
 };
 
 #endif  // SCOPED_FD_H_included
diff --git a/libcore/luni/src/main/native/ifaddrs-android.h b/libcore/luni/src/main/native/ifaddrs-android.h
index de87b02..1b9a9ef 100644
--- a/libcore/luni/src/main/native/ifaddrs-android.h
+++ b/libcore/luni/src/main/native/ifaddrs-android.h
@@ -136,6 +136,11 @@
         }
         return NULL;
     }
+
+private:
+    // Disallow copy and assignment.
+    ifaddrs(const ifaddrs&);
+    void operator=(const ifaddrs&);
 };
 
 // FIXME: use iovec instead.
diff --git a/libcore/luni/src/main/native/java_io_File.cpp b/libcore/luni/src/main/native/java_io_File.cpp
index 586ebbe..3006275 100644
--- a/libcore/luni/src/main/native/java_io_File.cpp
+++ b/libcore/luni/src/main/native/java_io_File.cpp
@@ -253,6 +253,10 @@
     DIR* mDirStream;
     dirent mEntry;
     bool mIsBad;
+
+    // Disallow copy and assignment.
+    ScopedReaddir(const ScopedReaddir&);
+    void operator=(const ScopedReaddir&);
 };
 
 // DirEntry and DirEntries is a minimal equivalent of std::forward_list
@@ -310,6 +314,10 @@
 private:
     size_t mSize;
     DirEntry* mHead;
+
+    // Disallow copy and assignment.
+    DirEntries(const DirEntries&);
+    void operator=(const DirEntries&);
 };
 
 // Reads the directory referred to by 'pathBytes', adding each directory entry
diff --git a/libcore/luni/src/main/native/java_net_InetAddress.cpp b/libcore/luni/src/main/native/java_net_InetAddress.cpp
index 04c18af..62318e9 100644
--- a/libcore/luni/src/main/native/java_net_InetAddress.cpp
+++ b/libcore/luni/src/main/native/java_net_InetAddress.cpp
@@ -83,8 +83,7 @@
         // Count results so we know how to size the output array.
         int addressCount = 0;
         for (addrInfo = addressList; addrInfo; addrInfo = addrInfo->ai_next) {
-            if (addrInfo->ai_family == AF_INET ||
-                addrInfo->ai_family == AF_INET6) {
+            if (addrInfo->ai_family == AF_INET || addrInfo->ai_family == AF_INET6) {
                 addressCount++;
             }
         }
@@ -94,7 +93,7 @@
         if (addressArray == NULL) {
             // Appropriate exception will be thrown.
             LOGE("getaddrinfo: could not allocate array of size %i", addressCount);
-            freeaddrinfo(addrInfo);
+            freeaddrinfo(addressList);
             return NULL;
         }
 
@@ -109,20 +108,17 @@
                 // Find the raw address length and start pointer.
                 case AF_INET6:
                     addressLength = 16;
-                    rawAddress =
-                        &((struct sockaddr_in6*) address)->sin6_addr.s6_addr;
+                    rawAddress = &((struct sockaddr_in6*) address)->sin6_addr.s6_addr;
                     logIpString(addrInfo, name);
                     break;
                 case AF_INET:
                     addressLength = 4;
-                    rawAddress =
-                        &((struct sockaddr_in*) address)->sin_addr.s_addr;
+                    rawAddress = &((struct sockaddr_in*) address)->sin_addr.s_addr;
                     logIpString(addrInfo, name);
                     break;
                 default:
                     // Unknown address family. Skip this address.
-                    LOGE("getaddrinfo: Unknown address family %d",
-                         addrInfo->ai_family);
+                    LOGE("getaddrinfo: Unknown address family %d", addrInfo->ai_family);
                     continue;
             }
 
@@ -130,13 +126,11 @@
             jbyteArray bytearray = env->NewByteArray(addressLength);
             if (bytearray == NULL) {
                 // Out of memory error will be thrown on return.
-                LOGE("getaddrinfo: Can't allocate %d-byte array",
-                     addressLength);
+                LOGE("getaddrinfo: Can't allocate %d-byte array", addressLength);
                 addressArray = NULL;
                 break;
             }
-            env->SetByteArrayRegion(bytearray, 0, addressLength,
-                                    (jbyte*) rawAddress);
+            env->SetByteArrayRegion(bytearray, 0, addressLength, (jbyte*) rawAddress);
             env->SetObjectArrayElement(addressArray, index, bytearray);
             env->DeleteLocalRef(bytearray);
             index++;
@@ -146,8 +140,7 @@
         jniThrowException(env, "java/lang/SecurityException",
             "Permission denied (maybe missing INTERNET permission)");
     } else {
-        jniThrowException(env, "java/net/UnknownHostException",
-                gai_strerror(result));
+        jniThrowException(env, "java/net/UnknownHostException", gai_strerror(result));
     }
 
     if (addressList) {
diff --git a/libcore/luni/src/main/native/java_net_NetworkInterface.cpp b/libcore/luni/src/main/native/java_net_NetworkInterface.cpp
index 724e988..4aea781 100644
--- a/libcore/luni/src/main/native/java_net_NetworkInterface.cpp
+++ b/libcore/luni/src/main/native/java_net_NetworkInterface.cpp
@@ -53,6 +53,11 @@
     }
     
     ifaddrs* list;
+
+private:
+    // Disallow copy and assignment.
+    ScopedInterfaceAddresses(const ScopedInterfaceAddresses&);
+    void operator=(const ScopedInterfaceAddresses&);
 };
 
 // TODO: add a header file for shared utilities like this.
diff --git a/libcore/xml/src/main/native/org_apache_harmony_xml_ExpatParser.cpp b/libcore/xml/src/main/native/org_apache_harmony_xml_ExpatParser.cpp
index b893309..d4fc557 100644
--- a/libcore/xml/src/main/native/org_apache_harmony_xml_ExpatParser.cpp
+++ b/libcore/xml/src/main/native/org_apache_harmony_xml_ExpatParser.cpp
@@ -562,6 +562,10 @@
             mPrefix = "";
         }
     }
+
+    // Disallow copy and assignment.
+    ExpatElementName(const ExpatElementName&);
+    void operator=(const ExpatElementName&);
 };
 
 /**