1d4e704
From 1f4bc63d8cc81747bf2685256c384e17450ca9c8 Mon Sep 17 00:00:00 2001
1d4e704
From: Peter Jones <pjones@redhat.com>
1d4e704
Date: Fri, 1 Aug 2014 08:03:49 -0400
1d4e704
Subject: [PATCH 18/22] Fix some leaked memory that shows up in valgrind.
1d4e704
1d4e704
Signed-off-by: Peter Jones <pjones@redhat.com>
1d4e704
---
1d4e704
 src/efibootmgr/efibootmgr.c | 29 ++++++++++++++++++++++-------
1d4e704
 1 file changed, 22 insertions(+), 7 deletions(-)
1d4e704
1d4e704
diff --git a/src/efibootmgr/efibootmgr.c b/src/efibootmgr/efibootmgr.c
1d4e704
index d3448da..c8cb6fd 100644
1d4e704
--- a/src/efibootmgr/efibootmgr.c
1d4e704
+++ b/src/efibootmgr/efibootmgr.c
1d4e704
@@ -44,6 +44,7 @@
1d4e704
 #include <unistd.h>
1d4e704
 #include <getopt.h>
1d4e704
 #include <efivar.h>
1d4e704
+#include <inttypes.h>
1d4e704
 #include "list.h"
1d4e704
 #include "efi.h"
1d4e704
 #include "efichar.h"
1d4e704
@@ -283,7 +284,7 @@ static int
1d4e704
 read_boot_order(efi_variable_t **boot_order)
1d4e704
 {
1d4e704
 	int rc;
1d4e704
-	efi_variable_t *new = NULL, *bo;
1d4e704
+	efi_variable_t *new = NULL, *bo = NULL;
1d4e704
 
1d4e704
 	if (*boot_order == NULL) {
1d4e704
 		new = calloc(1, sizeof (**boot_order));
1d4e704
@@ -299,12 +300,15 @@ read_boot_order(efi_variable_t **boot_order)
1d4e704
 	if (rc < 0 && new != NULL) {
1d4e704
 		free(new);
1d4e704
 		*boot_order = NULL;
1d4e704
+		bo = NULL;
1d4e704
 	}
1d4e704
 
1d4e704
-	/* latest apple firmware sets high bit which appears invalid
1d4e704
-	 * to the linux kernel if we write it back so lets zero it out
1d4e704
-	 * if it is set since it would be invalid to set it anyway */
1d4e704
-	bo->attributes = bo->attributes & ~(1 << 31);
1d4e704
+	if (bo) {
1d4e704
+		/* latest apple firmware sets high bit which appears invalid
1d4e704
+		 * to the linux kernel if we write it back so lets zero it out
1d4e704
+		 * if it is set since it would be invalid to set it anyway */
1d4e704
+		bo->attributes = bo->attributes & ~(1 << 31);
1d4e704
+	}
1d4e704
 	return rc;
1d4e704
 }
1d4e704
 
1d4e704
@@ -401,11 +405,15 @@ read_boot_u16(const char *name)
1d4e704
 	if (rc < 0)
1d4e704
 		return rc;
1d4e704
 	if (data_size != 2) {
1d4e704
+		if (data != NULL)
1d4e704
+			free(data);
1d4e704
 		errno = EINVAL;
1d4e704
 		return -1;
1d4e704
 	}
1d4e704
 
1d4e704
 	rc = data[0];
1d4e704
+	if (data != NULL)
1d4e704
+		free(data);
1d4e704
 	return rc;
1d4e704
 }
1d4e704
 
1d4e704
@@ -746,6 +754,11 @@ show_boot_order()
1d4e704
 
1d4e704
 	rc = read_boot_order(&boot_order);
1d4e704
 
1d4e704
+	if (rc < 0 && errno == ENOENT) {
1d4e704
+		boot_order = calloc(1, sizeof (*boot_order));
1d4e704
+		rc = boot_order ? 0 : -1;
1d4e704
+	}
1d4e704
+
1d4e704
 	if (rc < 0) {
1d4e704
 		perror("show_boot_order()");
1d4e704
 		return;
1d4e704
@@ -755,9 +768,11 @@ show_boot_order()
1d4e704
 	   boot order.  First add our entry, then copy the old array.
1d4e704
 	*/
1d4e704
 	data = (uint16_t *)boot_order->data;
1d4e704
-	if (boot_order->data_size)
1d4e704
+	if (boot_order->data_size) {
1d4e704
 		unparse_boot_order(data, boot_order->data_size / sizeof(uint16_t));
1d4e704
-
1d4e704
+		free(boot_order->data);
1d4e704
+	}
1d4e704
+	free(boot_order);
1d4e704
 }
1d4e704
 
1d4e704
 static int
1d4e704
-- 
1d4e704
1.9.3
1d4e704