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

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