Blame SOURCES/e2fsprogs-1.45.6-ss_create_invocation-fix-error-handling-when-memory-.patch

a77133
From e46e4a8c3c298ec05092008966072d73437c9728 Mon Sep 17 00:00:00 2001
a77133
From: Wu Guanghao <wuguanghao3@huawei.com>
a77133
Date: Wed, 28 Jul 2021 09:56:46 +0800
a77133
Subject: [PATCH 36/46] ss_create_invocation: fix error handling when memory
a77133
 allocation fails
a77133
Content-Type: text/plain
a77133
a77133
In ss_create_invocation(), it is necessary to check whether
a77133
returned by malloc is a null pointer.
a77133
a77133
Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
a77133
Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
a77133
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
a77133
Signed-off-by: Lukas Czerner <lczerner@redhat.com>
a77133
---
a77133
 lib/ss/invocation.c | 42 +++++++++++++++++++++++++++++++++---------
a77133
 1 file changed, 33 insertions(+), 9 deletions(-)
a77133
a77133
diff --git a/lib/ss/invocation.c b/lib/ss/invocation.c
a77133
index 457e7a2c..bf5ea0ce 100644
a77133
--- a/lib/ss/invocation.c
a77133
+++ b/lib/ss/invocation.c
a77133
@@ -26,29 +26,33 @@ int ss_create_invocation(const char *subsystem_name, const char *version_string,
a77133
 			 void *info_ptr, ss_request_table *request_table_ptr,
a77133
 			 int *code_ptr)
a77133
 {
a77133
-	register int sci_idx;
a77133
-	register ss_data *new_table;
a77133
-	register ss_data **table;
a77133
+	int sci_idx;
a77133
+	ss_data *new_table = NULL;
a77133
+	ss_data **table = NULL;
a77133
+	ss_data **realloc_table = NULL;
a77133
 
a77133
 	*code_ptr = 0;
a77133
 	table = _ss_table;
a77133
 	new_table = (ss_data *) malloc(sizeof(ss_data));
a77133
+	if (!new_table)
a77133
+		goto out;
a77133
 
a77133
 	if (table == (ss_data **) NULL) {
a77133
 		table = (ss_data **) malloc(2 * size);
a77133
+		if (!table)
a77133
+			goto out;
a77133
 		table[0] = table[1] = (ss_data *)NULL;
a77133
 	}
a77133
 	initialize_ss_error_table ();
a77133
 
a77133
 	for (sci_idx = 1; table[sci_idx] != (ss_data *)NULL; sci_idx++)
a77133
 		;
a77133
-	table = (ss_data **) realloc((char *)table,
a77133
+	realloc_table = (ss_data **) realloc((char *)table,
a77133
 				     ((unsigned)sci_idx+2)*size);
a77133
-	if (table == NULL) {
a77133
-		*code_ptr = ENOMEM;
a77133
-		free(new_table);
a77133
-		return 0;
a77133
-	}
a77133
+	if (realloc_table == NULL)
a77133
+		goto out;
a77133
+
a77133
+	table = realloc_table;
a77133
 	table[sci_idx+1] = (ss_data *) NULL;
a77133
 	table[sci_idx] = new_table;
a77133
 
a77133
@@ -57,9 +61,15 @@ int ss_create_invocation(const char *subsystem_name, const char *version_string,
a77133
 	new_table->argv = (char **)NULL;
a77133
 	new_table->current_request = (char *)NULL;
a77133
 	new_table->info_dirs = (char **)malloc(sizeof(char *));
a77133
+	if (!new_table->info_dirs)
a77133
+		goto out;
a77133
+
a77133
 	*new_table->info_dirs = (char *)NULL;
a77133
 	new_table->info_ptr = info_ptr;
a77133
 	new_table->prompt = malloc((unsigned)strlen(subsystem_name)+4);
a77133
+	if (!new_table->prompt)
a77133
+		goto out;
a77133
+
a77133
 	strcpy(new_table->prompt, subsystem_name);
a77133
 	strcat(new_table->prompt, ":  ");
a77133
 #ifdef silly
a77133
@@ -71,6 +81,9 @@ int ss_create_invocation(const char *subsystem_name, const char *version_string,
a77133
 	new_table->flags.abbrevs_disabled = 0;
a77133
 	new_table->rqt_tables =
a77133
 		(ss_request_table **) calloc(2, sizeof(ss_request_table *));
a77133
+	if (!new_table->rqt_tables)
a77133
+		goto out;
a77133
+
a77133
 	*(new_table->rqt_tables) = request_table_ptr;
a77133
 	*(new_table->rqt_tables+1) = (ss_request_table *) NULL;
a77133
 
a77133
@@ -85,6 +98,17 @@ int ss_create_invocation(const char *subsystem_name, const char *version_string,
a77133
 	ss_get_readline(sci_idx);
a77133
 #endif
a77133
 	return(sci_idx);
a77133
+
a77133
+out:
a77133
+	if (new_table) {
a77133
+		free(new_table->prompt);
a77133
+		free(new_table->info_dirs);
a77133
+	}
a77133
+	free(new_table);
a77133
+	free(table);
a77133
+	*code_ptr = ENOMEM;
a77133
+	return 0;
a77133
+
a77133
 }
a77133
 
a77133
 void
a77133
-- 
a77133
2.35.1
a77133