Fix a bunch of small system/core bugs.

Missing frees in:
  adb/file_sync_client.c
  fastboot/fastboot.c
  libsparse/output_file.c

Missing closedirs in:
  adb/file_sync_service.c
  cpio/mkbootfs.c
  libcutils/dir_hash.c

Potential buffer overrun in:
  gpttool/gpttool.c

Incorrect NULL check in:
  libsparse/backed_block.c

Bug: https://code.google.com/p/android/issues/detail?id=61564
Change-Id: If97838a9e73a77aef7f416c31c237ce1fca4ce21
diff --git a/adb/file_sync_client.c b/adb/file_sync_client.c
index 354d0fb..9fec081 100644
--- a/adb/file_sync_client.c
+++ b/adb/file_sync_client.c
@@ -642,8 +642,8 @@
             ci = mkcopyinfo(lpath, rpath, name, 0);
             if(lstat(ci->src, &st)) {
                 fprintf(stderr,"cannot stat '%s': %s\n", ci->src, strerror(errno));
+                free(ci);
                 closedir(d);
-
                 return -1;
             }
             if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
diff --git a/adb/file_sync_service.c b/adb/file_sync_service.c
index d3e841b..f24f14c 100644
--- a/adb/file_sync_service.c
+++ b/adb/file_sync_service.c
@@ -110,6 +110,7 @@
 
             if(writex(s, &msg.dent, sizeof(msg.dent)) ||
                writex(s, de->d_name, len)) {
+                closedir(d);
                 return -1;
             }
         }
diff --git a/cpio/mkbootfs.c b/cpio/mkbootfs.c
index 3569e27..7d3740c 100644
--- a/cpio/mkbootfs.c
+++ b/cpio/mkbootfs.c
@@ -220,6 +220,8 @@
         free(names[i]);
     }
     free(names);
+
+    closedir(d);
 }
 
 static void _archive(char *in, char *out, int ilen, int olen)
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index f339988..b9b3c92 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -21,6 +21,7 @@
 LOCAL_SRC_FILES := protocol.c engine.c bootimg.c fastboot.c util.c
 LOCAL_MODULE := fastboot
 LOCAL_MODULE_TAGS := debug
+LOCAL_CFLAGS += -std=gnu99
 
 ifeq ($(HOST_OS),linux)
   LOCAL_SRC_FILES += usb_linux.c util_linux.c
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index 70b838f..da2af41 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -449,7 +449,13 @@
 
     for(n = 0; n < count; n++) {
         out[n] = strdup(strip(val[n]));
-        if (out[n] == 0) return -1;
+        if (out[n] == 0) {
+            for(size_t i = 0; i < n; ++i) {
+                free((char*) out[i]);
+            }
+            free(out);
+            return -1;
+        }
     }
 
     fb_queue_require(prod, name, invert, n, out);
diff --git a/gpttool/gpttool.c b/gpttool/gpttool.c
index 05d5177..d3f08fe 100644
--- a/gpttool/gpttool.c
+++ b/gpttool/gpttool.c
@@ -161,7 +161,7 @@
 {
 	struct efi_entry *entry = ptbl->entry;
 	unsigned n, m;
-	char name[EFI_NAMELEN];
+	char name[EFI_NAMELEN + 1];
 
 	fprintf(stderr,"ptn  start block   end block     name\n");
 	fprintf(stderr,"---- ------------- ------------- --------------------\n");
diff --git a/libcutils/dir_hash.c b/libcutils/dir_hash.c
index be14af6..098b5db 100644
--- a/libcutils/dir_hash.c
+++ b/libcutils/dir_hash.c
@@ -159,6 +159,7 @@
 
             free(name);
             free(node);
+            closedir(d);
             return -1;
         }
 
diff --git a/libsparse/backed_block.c b/libsparse/backed_block.c
index dfb217b..3e72b57 100644
--- a/libsparse/backed_block.c
+++ b/libsparse/backed_block.c
@@ -370,7 +370,7 @@
 	}
 
 	new_bb = malloc(sizeof(struct backed_block));
-	if (bb == NULL) {
+	if (new_bb == NULL) {
 		return -ENOMEM;
 	}
 
diff --git a/libsparse/output_file.c b/libsparse/output_file.c
index 2428022..a28b0a5 100644
--- a/libsparse/output_file.c
+++ b/libsparse/output_file.c
@@ -722,10 +722,12 @@
 	}
 	pos = lseek64(fd, offset, SEEK_SET);
 	if (pos < 0) {
+                free(data);
 		return -errno;
 	}
 	ret = read_all(fd, data, len);
 	if (ret < 0) {
+                free(data);
 		return ret;
 	}
 	ptr = data;