[Concept,5/7] efi: Refactor error handling in efi_dump_var_all()

Message ID 20250821153528.141740-6-sjg@u-boot.org
State New
Headers
Series efi: Improvements to env print -e |

Commit Message

Simon Glass Aug. 21, 2025, 3:35 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Rather than freeing the variable in several places, put it at the end.
Assume failure by default, returning success only if all steps passed.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 cmd/nvedit_efi.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
  

Patch

diff --git a/cmd/nvedit_efi.c b/cmd/nvedit_efi.c
index 0e4abf49fc0..f2bba42850d 100644
--- a/cmd/nvedit_efi.c
+++ b/cmd/nvedit_efi.c
@@ -150,6 +150,7 @@  static int efi_dump_var_all(int argc,  char *const argv[],
 	efi_guid_t guid;
 	efi_status_t ret;
 	bool match = false;
+	bool ok = false;
 	u16 *name, *p;
 
 	buf_size = 128;
@@ -166,18 +167,14 @@  static int efi_dump_var_all(int argc,  char *const argv[],
 		if (ret == EFI_BUFFER_TOO_SMALL) {
 			buf_size = size;
 			p = realloc(name, buf_size);
-			if (!p) {
-				free(name);
-				return CMD_RET_FAILURE;
-			}
+			if (!p)
+				goto fail;
 			name = p;
 			ret = efi_get_next_variable_name_int(&size, name,
 							     &guid);
 		}
-		if (ret != EFI_SUCCESS) {
-			free(name);
-			return CMD_RET_FAILURE;
-		}
+		if (ret != EFI_SUCCESS)
+			goto fail;
 
 		if (guid_p && guidcmp(guid_p, &guid))
 			continue;
@@ -186,14 +183,18 @@  static int efi_dump_var_all(int argc,  char *const argv[],
 			efi_dump_single_var(name, &guid, verbose, nodump);
 		}
 	}
-	free(name);
 
 	if (!match && argc == 1) {
 		printf("Error: \"%s\" not defined\n", argv[0]);
-		return CMD_RET_FAILURE;
+		goto done;
 	}
 
-	return CMD_RET_SUCCESS;
+	ok = true;
+fail:
+done:
+	free(name);
+
+	return ok ? 0 : CMD_RET_FAILURE;
 }
 
 /**