Blame SOURCES/008125849f94a5c7d8ccbd56ed1156f49169d273.patch

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