Blame SOURCES/008125849f94a5c7d8ccbd56ed1156f49169d273.patch

714f59
commit 008125849f94a5c7d8ccbd56ed1156f49169d273
714f59
Author: Shiju Jose <shiju.jose@huawei.com>
714f59
Date:   Wed Oct 16 17:33:55 2019 +0100
714f59
714f59
    rasdaemon: fix cleanup issues in ras-events.c:read_ras_event_all_cpus()
714f59
    
714f59
    This patch fix memory leaks and close the open files if the
714f59
    open_trace() or read(fds[i].fd, page, pdata[i].ras->page_size)
714f59
    function calls fail.
714f59
    
714f59
    Signed-off-by: Shiju Jose <shiju.jose@huawei.com>
714f59
    Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
714f59
714f59
diff --git a/ras-events.c b/ras-events.c
714f59
index 3cdac19..d1773b1 100644
714f59
--- a/ras-events.c
714f59
+++ b/ras-events.c
714f59
@@ -353,6 +353,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata,
714f59
 	struct pollfd fds[n_cpus];
714f59
 	int warnonce[n_cpus];
714f59
 	char pipe_raw[PATH_MAX];
714f59
+	int legacy_kernel = 0;
714f59
 #if 0
714f59
 	int need_sleep = 0;
714f59
 #endif
714f59
@@ -372,6 +373,9 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata,
714f59
 		return -ENOMEM;
714f59
 	}
714f59
 
714f59
+	for (i = 0; i < n_cpus; i++)
714f59
+		fds[i].fd = -1;
714f59
+
714f59
 	for (i = 0; i < n_cpus; i++) {
714f59
 		fds[i].events = POLLIN;
714f59
 
714f59
@@ -382,9 +386,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata,
714f59
 		fds[i].fd = open_trace(pdata[0].ras, pipe_raw, O_RDONLY);
714f59
 		if (fds[i].fd < 0) {
714f59
 			log(TERM, LOG_ERR, "Can't open trace_pipe_raw\n");
714f59
-			kbuffer_free(kbuf);
714f59
-			free(page);
714f59
-			return -EINVAL;
714f59
+			goto error;
714f59
 		}
714f59
 	}
714f59
 
714f59
@@ -416,7 +418,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata,
714f59
 			size = read(fds[i].fd, page, pdata[i].ras->page_size);
714f59
 			if (size < 0) {
714f59
 				log(TERM, LOG_WARNING, "read\n");
714f59
-				return -1;
714f59
+				goto error;
714f59
 			} else if (size > 0) {
714f59
 				kbuffer_load_subbuffer(kbuf, page);
714f59
 
714f59
@@ -441,6 +443,7 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata,
714f59
 		 */
714f59
 		if (count_nready == n_cpus) {
714f59
 			/* Should only happen with legacy kernels */
714f59
+			legacy_kernel = 1;
714f59
 			break;
714f59
 		}
714f59
 #endif
714f59
@@ -449,12 +452,18 @@ static int read_ras_event_all_cpus(struct pthread_data *pdata,
714f59
 	/* poll() is not supported. We need to fallback to the old way */
714f59
 	log(TERM, LOG_INFO,
714f59
 	    "Old kernel detected. Stop listening and fall back to pthread way.\n");
714f59
+error:
714f59
 	kbuffer_free(kbuf);
714f59
 	free(page);
714f59
-	for (i = 0; i < n_cpus; i++)
714f59
-		close(fds[i].fd);
714f59
+	for (i = 0; i < n_cpus; i++) {
714f59
+		if (fds[i].fd > 0)
714f59
+			close(fds[i].fd);
714f59
+	}
714f59
 
714f59
-	return -255;
714f59
+	if (legacy_kernel)
714f59
+		return -255;
714f59
+	else
714f59
+		return -1;
714f59
 }
714f59
 
714f59
 static int read_ras_event(int fd,