Re-apply several tools fixes lost by the directory rearrangement.

Leaks on error in tools/aapt/Images.cpp.
https://code.google.com/p/android/issues/detail?id=61552

Two missing fclose calls in tools/aapt/Resource.cpp.
https://code.google.com/p/android/issues/detail?id=61553

Missing fclose in tools/aidl/aidl.cpp.
https://code.google.com/p/android/issues/detail?id=61554

Change-Id: I56ce144958296961b77354815efc1a245564594b
diff --git a/tools/aapt/Images.cpp b/tools/aapt/Images.cpp
index 8947597..b1a548e 100644
--- a/tools/aapt/Images.cpp
+++ b/tools/aapt/Images.cpp
@@ -462,8 +462,8 @@
 
     int maxSizeXDivs = W * sizeof(int32_t);
     int maxSizeYDivs = H * sizeof(int32_t);
-    int32_t* xDivs = (int32_t*) malloc(maxSizeXDivs);
-    int32_t* yDivs = (int32_t*) malloc(maxSizeYDivs);
+    int32_t* xDivs = image->info9Patch.xDivs = (int32_t*) malloc(maxSizeXDivs);
+    int32_t* yDivs = image->info9Patch.yDivs = (int32_t*) malloc(maxSizeYDivs);
     uint8_t  numXDivs = 0;
     uint8_t  numYDivs = 0;
     int8_t numColors;
@@ -520,6 +520,10 @@
         goto getout;
     }
 
+    // Copy patch size data into image...
+    image->info9Patch.numXDivs = numXDivs;
+    image->info9Patch.numYDivs = numYDivs;
+
     // Find left and right of padding area...
     if (get_horizontal_ticks(image->rows[H-1], W, transparent, false, &image->info9Patch.paddingLeft,
                              &image->info9Patch.paddingRight, &errorMsg, NULL, false) != NO_ERROR) {
@@ -555,12 +559,6 @@
                 image->layoutBoundsRight, image->layoutBoundsBottom));
     }
 
-    // Copy patch data into image
-    image->info9Patch.numXDivs = numXDivs;
-    image->info9Patch.numYDivs = numYDivs;
-    image->info9Patch.xDivs = xDivs;
-    image->info9Patch.yDivs = yDivs;
-
     // If padding is not yet specified, take values from size.
     if (image->info9Patch.paddingLeft < 0) {
         image->info9Patch.paddingLeft = xDivs[0];
diff --git a/tools/aapt/Resource.cpp b/tools/aapt/Resource.cpp
index 08ad7a0..8f43661 100644
--- a/tools/aapt/Resource.cpp
+++ b/tools/aapt/Resource.cpp
@@ -2219,10 +2219,10 @@
 
         status_t err = writeSymbolClass(fp, assets, includePrivate, symbols,
                 className, 0, bundle->getNonConstantId());
+        fclose(fp);
         if (err != NO_ERROR) {
             return err;
         }
-        fclose(fp);
 
         if (textSymbolsDest != NULL && R == className) {
             String8 textDest(textSymbolsDest);
@@ -2241,10 +2241,10 @@
 
             status_t err = writeTextSymbolClass(fp, assets, includePrivate, symbols,
                     className);
+            fclose(fp);
             if (err != NO_ERROR) {
                 return err;
             }
-            fclose(fp);
         }
 
         // If we were asked to generate a dependency file, we'll go ahead and add this R.java
diff --git a/tools/aidl/aidl.cpp b/tools/aidl/aidl.cpp
index b8a4803..9c1867e 100644
--- a/tools/aidl/aidl.cpp
+++ b/tools/aidl/aidl.cpp
@@ -847,6 +847,7 @@
         else {
             fprintf(stderr, "%s:%d: bad type in line: %s\n",
                     filename.c_str(), lineno, line);
+            fclose(f);
             return 1;
         }
         err = gather_types(filename.c_str(), doc);