Blame SOURCES/0039-Fix-a-case-clang-analyzer-found-where-we-may-try-to-.patch

d5c737
From 9819ec7325089d325ff13af3c3d615209f3fb2c9 Mon Sep 17 00:00:00 2001
d5c737
From: Peter Jones <pjones@redhat.com>
d5c737
Date: Tue, 18 Jun 2019 15:54:58 -0400
d5c737
Subject: [PATCH 39/63] Fix a case clang-analyzer found where we may try to
d5c737
 parse a NULL
d5c737
d5c737
I don't think this is something that can *actually* happen - it didn't
d5c737
trigger before save_variable() was added, and the save_variable() path
d5c737
that calls this calls validate_name() immediately prior to this call.
d5c737
validate_name() calls exit() if it's NULL.  But that's weird as well,
d5c737
because that's the same pattern all the other users of parse_name() use.
d5c737
d5c737
Anyway, this patch expands validate_name() and moves it into
d5c737
parse_name() so we don't need to call it from everywhere when we're just
d5c737
calling the two in a row anyway.
d5c737
d5c737
Signed-off-by: Peter Jones <pjones@redhat.com>
d5c737
---
d5c737
 src/efivar.c | 45 ++++++++++++++++++++++++++++++---------------
d5c737
 1 file changed, 30 insertions(+), 15 deletions(-)
d5c737
d5c737
diff --git a/src/efivar.c b/src/efivar.c
d5c737
index 885a9af864b..8b1da8888f6 100644
d5c737
--- a/src/efivar.c
d5c737
+++ b/src/efivar.c
d5c737
@@ -95,6 +95,34 @@ show_errors(void)
d5c737
 	}
d5c737
 }
d5c737
 
d5c737
+static inline void
d5c737
+validate_name(const char *name)
d5c737
+{
d5c737
+	if (name == NULL) {
d5c737
+err:
d5c737
+		warnx("Invalid variable name \"%s\"",
d5c737
+		      (name == NULL) ? "(null)" : name);
d5c737
+		show_errors();
d5c737
+		exit(1);
d5c737
+	}
d5c737
+	if (name[0] == '{') {
d5c737
+		const char *next = strchr(name+1, '}');
d5c737
+		if (!next)
d5c737
+			goto err;
d5c737
+		if (next[1] != '-')
d5c737
+			goto err;
d5c737
+		if (next[2] == '\000')
d5c737
+			goto err;
d5c737
+	} else {
d5c737
+		if (strlen(name) < 38)
d5c737
+			goto err;
d5c737
+		if (name[8] != '-' || name[13] != '-' ||
d5c737
+		    name[18] != '-' || name[23] != '-' ||
d5c737
+		    name[36] != '-')
d5c737
+			goto err;
d5c737
+	}
d5c737
+}
d5c737
+
d5c737
 static void
d5c737
 list_all_variables(void)
d5c737
 {
d5c737
@@ -124,6 +152,8 @@ parse_name(const char *guid_name, char **name, efi_guid_t *guid)
d5c737
 
d5c737
 	const char *left, *right;
d5c737
 
d5c737
+	validate_name(guid_name);
d5c737
+
d5c737
 	left = strchr(guid_name, '{');
d5c737
 	right = strchr(guid_name, '}');
d5c737
 	if (left && right) {
d5c737
@@ -408,16 +438,6 @@ edit_variable(const char *guid_name, void *data, size_t data_size,
d5c737
 	}
d5c737
 }
d5c737
 
d5c737
-static void
d5c737
-validate_name(const char *name)
d5c737
-{
d5c737
-	if (name == NULL) {
d5c737
-		fprintf(stderr, "Invalid variable name\n");
d5c737
-		show_errors();
d5c737
-		exit(1);
d5c737
-	}
d5c737
-}
d5c737
-
d5c737
 static void
d5c737
 prepare_data(const char *filename, uint8_t **data, size_t *data_size)
d5c737
 {
d5c737
@@ -588,21 +608,17 @@ int main(int argc, char *argv[])
d5c737
 			list_all_variables();
d5c737
 			break;
d5c737
 		case ACTION_PRINT:
d5c737
-			validate_name(guid_name);
d5c737
 			show_variable(guid_name, SHOW_VERBOSE);
d5c737
 			break;
d5c737
 		case ACTION_PRINT_DEC | ACTION_PRINT:
d5c737
-			validate_name(guid_name);
d5c737
 			show_variable(guid_name, SHOW_DECIMAL);
d5c737
 			break;
d5c737
 		case ACTION_APPEND | ACTION_PRINT:
d5c737
-			validate_name(guid_name);
d5c737
 			prepare_data(infile, &data, &data_size);
d5c737
 			edit_variable(guid_name, data, data_size, attributes,
d5c737
 				      EDIT_APPEND);
d5c737
 			break;
d5c737
 		case ACTION_WRITE | ACTION_PRINT:
d5c737
-			validate_name(guid_name);
d5c737
 			prepare_data(infile, &data, &data_size);
d5c737
 			edit_variable(guid_name, data, data_size, attributes,
d5c737
 				      EDIT_WRITE);
d5c737
@@ -653,7 +669,6 @@ int main(int argc, char *argv[])
d5c737
 
d5c737
 				efi_variable_free(var, false);
d5c737
 			} else {
d5c737
-				validate_name(guid_name);
d5c737
 				save_variable(guid_name, outfile, dmpstore);
d5c737
 			}
d5c737
 			break;
d5c737
-- 
d5c737
2.26.2
d5c737