Fix a possible file descriptor leakage

Bug: 15279918
Change-Id: I7909a53f9028d2f445fb97a0a4293f36b3c012dd
diff --git a/runtime/native/dalvik_system_DexFile.cc b/runtime/native/dalvik_system_DexFile.cc
index a0a294a..8588c3a 100644
--- a/runtime/native/dalvik_system_DexFile.cc
+++ b/runtime/native/dalvik_system_DexFile.cc
@@ -35,6 +35,7 @@
 #include "profiler.h"
 #include "runtime.h"
 #include "scoped_thread_state_change.h"
+#include "ScopedFd.h"
 #include "ScopedLocalRef.h"
 #include "ScopedUtfChars.h"
 #include "well_known_classes.h"
@@ -220,8 +221,8 @@
 
 // Copy a profile file
 static void CopyProfileFile(const char* oldfile, const char* newfile) {
-  int fd = open(oldfile, O_RDONLY);
-  if (fd < 0) {
+  ScopedFd fd(open(oldfile, O_RDONLY));
+  if (fd.get() == -1) {
     // If we can't open the file show the uid:gid of the this process to allow
     // diagnosis of the problem.
     LOG(ERROR) << "Failed to open profile file " << oldfile<< ".  My uid:gid is "
@@ -230,8 +231,8 @@
   }
 
   // Create the copy with rw------- (only accessible by system)
-  int fd2 = open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600);
-  if (fd2 < 0) {
+  ScopedFd fd2(open(newfile, O_WRONLY|O_CREAT|O_TRUNC, 0600));
+  if (fd2.get()  == -1) {
     // If we can't open the file show the uid:gid of the this process to allow
     // diagnosis of the problem.
     LOG(ERROR) << "Failed to create/write prev profile file " << newfile << ".  My uid:gid is "
@@ -240,14 +241,12 @@
   }
   char buf[4096];
   while (true) {
-    int n = read(fd, buf, sizeof(buf));
+    int n = read(fd.get(), buf, sizeof(buf));
     if (n <= 0) {
       break;
     }
-    write(fd2, buf, n);
+    write(fd2.get(), buf, n);
   }
-  close(fd);
-  close(fd2);
 }
 
 static double GetDoubleProperty(const char* property, double minValue, double maxValue, double defaultValue) {