Blame SOURCES/irqbalance-1.4.0-Fix-several-memleak-problems-found-by-covscan.patch

a48aee
From 9ed5c269bd59c95f41829aedf0520930c97b08d3 Mon Sep 17 00:00:00 2001
a48aee
From: Kairui Song <kasong@redhat.com>
a48aee
Date: Thu, 30 Aug 2018 17:45:53 +0800
a48aee
Subject: Fix several memleak problems found by covscan
a48aee
a48aee
Some memleak issues is found by static analysis tools, and can confirm
a48aee
irqbalance is leaking memory slowly when there are incomming connection
a48aee
to socket.
a48aee
a48aee
This patch could solve the memleak problem.
a48aee
---
a48aee
 irqbalance.c       | 16 ++++++++++++----
a48aee
 ui/irqbalance-ui.c | 31 +++++++++++++++++++++++++++----
a48aee
 ui/ui.c            |  2 ++
a48aee
 3 files changed, 41 insertions(+), 8 deletions(-)
a48aee
a48aee
diff --git a/irqbalance.c b/irqbalance.c
a48aee
index 6412447..4b3de54 100644
a48aee
--- a/irqbalance.c
a48aee
+++ b/irqbalance.c
a48aee
@@ -385,11 +385,11 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
a48aee
 		sock = accept(fd, NULL, NULL);
a48aee
 		if (sock < 0) {
a48aee
 			log(TO_ALL, LOG_WARNING, "Connection couldn't be accepted.\n");
a48aee
-			return TRUE;
a48aee
+			goto out;
a48aee
 		}
a48aee
 		if ((recv_size = recvmsg(sock, &msg, 0)) < 0) {
a48aee
 			log(TO_ALL, LOG_WARNING, "Error while receiving data.\n");
a48aee
-			return TRUE;
a48aee
+			goto out;
a48aee
 		}
a48aee
 		cmsg = CMSG_FIRSTHDR(&msg;;
a48aee
 		if ((cmsg->cmsg_level == SOL_SOCKET) &&
a48aee
@@ -401,7 +401,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
a48aee
 		}
a48aee
 		if (!valid_user) {
a48aee
 			log(TO_ALL, LOG_INFO, "Permission denied for user to connect to socket.\n");
a48aee
-			return TRUE;
a48aee
+			goto out;
a48aee
 		}
a48aee
 
a48aee
 		if (!strncmp(buff, "stats", strlen("stats"))) {
a48aee
@@ -421,6 +421,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
a48aee
 				if (new_iterval >= 1) {
a48aee
 					sleep_interval = new_iterval;
a48aee
 				}
a48aee
+				free(sleep_string);
a48aee
 			} else if (!(strncmp(buff + strlen("settings "), "ban irqs ",
a48aee
 							strlen("ban irqs ")))) {
a48aee
 				char *end;
a48aee
@@ -432,12 +433,14 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
a48aee
 				cl_banned_irqs = NULL;
a48aee
 				need_rescan = 1;
a48aee
 				if (!strncmp(irq_string, "NONE", strlen("NONE"))) {
a48aee
-					return TRUE;
a48aee
+					free(irq_string);
a48aee
+					goto out;
a48aee
 				}
a48aee
 				int irq = strtoul(irq_string, &end, 10);
a48aee
 				do {
a48aee
 					add_cl_banned_irq(irq);
a48aee
 				} while((irq = strtoul(end, &end, 10)));
a48aee
+				free(irq_string);
a48aee
 			} else if (!(strncmp(buff + strlen("settings "), "cpus ",
a48aee
 							strlen("cpus")))) {
a48aee
 				char *cpu_ban_string = malloc(
a48aee
@@ -449,6 +452,7 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
a48aee
 					banned_cpumask_from_ui = NULL;
a48aee
 				}
a48aee
 				need_rescan = 1;
a48aee
+				free(cpu_ban_string);
a48aee
 			}
a48aee
 		}
a48aee
 		if (!strncmp(buff, "setup", strlen("setup"))) {
a48aee
@@ -463,10 +467,14 @@ gboolean sock_handle(gint fd, GIOCondition condition, gpointer user_data __attri
a48aee
 			snprintf(setup + strlen(setup), strlen(banned) + 7 + 1,
a48aee
 					"BANNED %s", banned);
a48aee
 			send(sock, setup, strlen(setup), 0);
a48aee
+			free(setup);
a48aee
 		}
a48aee
 
a48aee
 		close(sock);
a48aee
 	}
a48aee
+
a48aee
+out:
a48aee
+	free(msg.msg_control);
a48aee
 	return TRUE;
a48aee
 }
a48aee
 
a48aee
diff --git a/ui/irqbalance-ui.c b/ui/irqbalance-ui.c
a48aee
index 3fc46af..99f2ca2 100644
a48aee
--- a/ui/irqbalance-ui.c
a48aee
+++ b/ui/irqbalance-ui.c
a48aee
@@ -41,6 +41,7 @@ struct msghdr * create_credentials_msg()
a48aee
 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
a48aee
 	memcpy(CMSG_DATA(cmsg), credentials, sizeof(struct ucred));
a48aee
 
a48aee
+	free(credentials);
a48aee
 	return msg;
a48aee
 }
a48aee
 
a48aee
@@ -87,6 +88,8 @@ void send_settings(char *data)
a48aee
 	sendmsg(socket_fd, msg, 0);
a48aee
 
a48aee
 	close(socket_fd);
a48aee
+	free(msg->msg_control);
a48aee
+	free(msg);
a48aee
 }
a48aee
 
a48aee
 char * get_data(char *string)
a48aee
@@ -115,6 +118,8 @@ char * get_data(char *string)
a48aee
 	int len = recv(socket_fd, data, 8192, 0);
a48aee
 	close(socket_fd);
a48aee
 	data[len] = '\0';
a48aee
+	free(msg->msg_control);
a48aee
+	free(msg);
a48aee
 	return data;
a48aee
 }
a48aee
 
a48aee
@@ -123,6 +128,7 @@ void parse_setup(char *setup_data)
a48aee
 	char *token, *ptr;
a48aee
 	int i,j;
a48aee
 	char *copy;
a48aee
+	irq_t *new_irq = NULL;
a48aee
 	if((setup_data == NULL) || (strlen(setup_data) == 0)) return;
a48aee
 	copy = strdup(setup_data);
a48aee
 	if (!copy)
a48aee
@@ -136,7 +142,7 @@ void parse_setup(char *setup_data)
a48aee
 	token = strtok_r(NULL, " ", &ptr);
a48aee
 	/* Parse banned IRQ data */
a48aee
 	while(!strncmp(token, "IRQ", strlen("IRQ"))) {
a48aee
-		irq_t *new_irq = malloc(sizeof(irq_t));
a48aee
+		new_irq = malloc(sizeof(irq_t));
a48aee
 		new_irq->vector = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
a48aee
 		token = strtok_r(NULL, " ", &ptr);
a48aee
 		if(strncmp(token, "LOAD", strlen("LOAD"))) goto out;
a48aee
@@ -151,6 +157,7 @@ void parse_setup(char *setup_data)
a48aee
 		new_irq->assigned_to = NULL;
a48aee
 		setup.banned_irqs = g_list_append(setup.banned_irqs, new_irq);
a48aee
 		token = strtok_r(NULL, " ", &ptr);
a48aee
+		new_irq = NULL;
a48aee
 	}
a48aee
 
a48aee
 	if(strncmp(token, "BANNED", strlen("BANNED"))) goto out;
a48aee
@@ -165,6 +172,7 @@ void parse_setup(char *setup_data)
a48aee
 								banned_cpu);
a48aee
 			}
a48aee
 		}
a48aee
+		free(map);
a48aee
 	
a48aee
 	}
a48aee
 	free(copy);
a48aee
@@ -173,6 +181,9 @@ void parse_setup(char *setup_data)
a48aee
 out: {
a48aee
 	/* Invalid data presented */
a48aee
 	printf("Invalid data sent.  Unexpected token: %s", token);
a48aee
+	if (new_irq) {
a48aee
+		free(new_irq);
a48aee
+	}
a48aee
 	free(copy);
a48aee
 	g_list_free(tree);
a48aee
 	exit(1);
a48aee
@@ -240,7 +251,9 @@ void parse_into_tree(char *data)
a48aee
 	cpu_node_t *parent = NULL;
a48aee
 	char *copy;
a48aee
 	tree = NULL;
a48aee
-	
a48aee
+	irq_t *new_irq = NULL;
a48aee
+	cpu_node_t *new = NULL;
a48aee
+
a48aee
 	if (!data || strlen(data) == 0)
a48aee
 		return;
a48aee
 
a48aee
@@ -255,7 +268,7 @@ void parse_into_tree(char *data)
a48aee
 			free(copy);
a48aee
 			 goto out;
a48aee
 		}
a48aee
-		cpu_node_t *new = malloc(sizeof(cpu_node_t));
a48aee
+		new = malloc(sizeof(cpu_node_t));
a48aee
 		new->irqs = NULL;
a48aee
 		new->children = NULL;
a48aee
 		new->cpu_list = NULL;
a48aee
@@ -279,7 +292,7 @@ void parse_into_tree(char *data)
a48aee
 
a48aee
 		/* Parse assigned IRQ data */
a48aee
 		while((token != NULL) && (!strncmp(token, "IRQ", strlen("IRQ")))) {
a48aee
-			irq_t *new_irq = malloc(sizeof(irq_t));
a48aee
+			new_irq = malloc(sizeof(irq_t));
a48aee
 			new_irq->vector = strtol(strtok_r(NULL, " ", &ptr), NULL, 10);
a48aee
 			token = strtok_r(NULL, " ", &ptr);
a48aee
 			if(strncmp(token, "LOAD", strlen("LOAD"))) goto out;
a48aee
@@ -293,6 +306,7 @@ void parse_into_tree(char *data)
a48aee
 			new_irq->is_banned = 0;
a48aee
 			new->irqs = g_list_append(new->irqs, new_irq);
a48aee
 			token = strtok_r(NULL, " ", &ptr);
a48aee
+			new_irq = NULL;
a48aee
 		}
a48aee
 
a48aee
 		if((token == NULL) || (strncmp(token, "IRQ", strlen("IRQ")))) {
a48aee
@@ -306,6 +320,8 @@ void parse_into_tree(char *data)
a48aee
 				parent = new;
a48aee
 			}
a48aee
 		}
a48aee
+
a48aee
+		new = NULL;
a48aee
 	}
a48aee
 	free(copy);
a48aee
 	for_each_node(tree, assign_cpu_lists, NULL);
a48aee
@@ -315,6 +331,12 @@ void parse_into_tree(char *data)
a48aee
 out: {
a48aee
 	/* Invalid data presented */
a48aee
 	printf("Invalid data sent.  Unexpected token: %s\n", token);
a48aee
+	if (new_irq) {
a48aee
+		free(new_irq);
a48aee
+	}
a48aee
+	if (new) {
a48aee
+		free(new);
a48aee
+	}
a48aee
 	g_list_free(tree);
a48aee
 	exit(1);
a48aee
 }
a48aee
@@ -330,6 +352,7 @@ gboolean rescan_tree(gpointer data __attribute__((unused)))
a48aee
 		display_tree();
a48aee
 	}
a48aee
 	free(setup_data);
a48aee
+	free(irqbalance_data);
a48aee
 	return TRUE;
a48aee
 }
a48aee
 
a48aee
diff --git a/ui/ui.c b/ui/ui.c
a48aee
index 4054f0e..06ec472 100644
a48aee
--- a/ui/ui.c
a48aee
+++ b/ui/ui.c
a48aee
@@ -71,6 +71,7 @@ char * check_control_in_sleep_input(int max_len, int column_offest, int line_off
a48aee
 			attrset(COLOR_PAIR(6));
a48aee
 			break;
a48aee
 		case 27:
a48aee
+			free(input_to);
a48aee
 			return NULL;
a48aee
 		default:
a48aee
 			input_to[iteration] = new;
a48aee
@@ -115,6 +116,7 @@ int get_valid_sleep_input(int column_offest)
a48aee
 				input);
a48aee
 			refresh();
a48aee
 		}
a48aee
+		free(input);
a48aee
 	}
a48aee
 
a48aee
 	attrset(COLOR_PAIR(1));
a48aee
-- 
a48aee
2.17.1
a48aee