Skip to contentSkip to conversation

Fix memory leak in function create_toolcontext

PATCH New Han Han 1 participant 1 message Apr 25, 2018

lore
Share & tools
lore

Han Han

author
#0 lore raw

PATCH 1/1 Fix memory leak in function create_toolcontext

Close stdin and stdout file handlers before create_toolcontext returns.

---
 lib/commands/toolcontext.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
lib/commands/toolcontext.c +12 -2
index a54f4d7b5..f81743540 100644
@@ -1838,6 +1838,8 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
 {
 	struct cmd_context *cmd;
 	int flags;
+    FILE **stdin_stream = NULL;
+    FILE **stdout_stream = NULL;
 
 #ifdef M_MMAP_MAX
 	mallopt(M_MMAP_MAX, 0);
@@ -1892,7 +1894,7 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
 		if (is_valid_fd(STDIN_FILENO) &&
 		    ((flags = fcntl(STDIN_FILENO, F_GETFL)) > 0) &&
 		    (flags & O_ACCMODE) != O_WRONLY) {
-			if (!reopen_standard_stream(&stdin, "r"))
+			if (!reopen_standard_stream(stdin_stream=&stdin, "r"))
 				goto_out;
 			if (setvbuf(stdin, cmd->linebuffer, _IOLBF, _linebuffer_size)) {
 				log_sys_error("setvbuf", "");
@@ -1903,7 +1905,7 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
 		if (is_valid_fd(STDOUT_FILENO) &&
 		    ((flags = fcntl(STDOUT_FILENO, F_GETFL)) > 0) &&
 		    (flags & O_ACCMODE) != O_RDONLY) {
-			if (!reopen_standard_stream(&stdout, "w"))
+			if (!reopen_standard_stream(stdin_stream=&stdout, "w"))
 				goto_out;
 			if (setvbuf(stdout, cmd->linebuffer + _linebuffer_size,
 				     _IOLBF, _linebuffer_size)) {
@@ -2015,6 +2017,14 @@ out:
 		cmd = NULL;
 	}
 
+    if (stdin_stream != NULL){
+        fclose(*stdin_stream);
+    }
+
+    if (stdout_stream != NULL){
+        fclose(*stdin_stream);
+    }
+
 	return cmd;
 }
 
Signature · 1 line
2.17.0