a7b456
From 69bc94779c2f035a9fffdb5327a54c3aeca73ed5 Mon Sep 17 00:00:00 2001
a7b456
From: Simon Kelley <simon@thekelleys.org.uk>
a7b456
Date: Wed, 14 Aug 2019 20:44:50 +0100
a7b456
Subject: [PATCH] Fix memory leak in helper.c
a7b456
a7b456
Thanks to Xu Mingjie <xumingjie1995@outlook.com> for spotting this.
a7b456
---
a7b456
 src/helper.c | 12 +++++++++---
a7b456
 1 file changed, 9 insertions(+), 3 deletions(-)
a7b456
a7b456
diff --git a/src/helper.c b/src/helper.c
a7b456
index 33ba120..c392eec 100644
a7b456
--- a/src/helper.c
a7b456
+++ b/src/helper.c
a7b456
@@ -80,7 +80,8 @@ int create_helper(int event_fd, int err_fd, uid_t uid, gid_t gid, long max_fd)
a7b456
   pid_t pid;
a7b456
   int i, pipefd[2];
a7b456
   struct sigaction sigact;
a7b456
-
a7b456
+  unsigned char *alloc_buff = NULL;
a7b456
+  
a7b456
   /* create the pipe through which the main program sends us commands,
a7b456
      then fork our process. */
a7b456
   if (pipe(pipefd) == -1 || !fix_fd(pipefd[1]) || (pid = fork()) == -1)
a7b456
@@ -186,11 +187,16 @@ int create_helper(int event_fd, int err_fd, uid_t uid, gid_t gid, long max_fd)
a7b456
       struct script_data data;
a7b456
       char *p, *action_str, *hostname = NULL, *domain = NULL;
a7b456
       unsigned char *buf = (unsigned char *)daemon->namebuff;
a7b456
-      unsigned char *end, *extradata, *alloc_buff = NULL;
a7b456
+      unsigned char *end, *extradata;
a7b456
       int is6, err = 0;
a7b456
       int pipeout[2];
a7b456
 
a7b456
-      free(alloc_buff);
a7b456
+      /* Free rarely-allocated memory from previous iteration. */
a7b456
+      if (alloc_buff)
a7b456
+	{
a7b456
+	  free(alloc_buff);
a7b456
+	  alloc_buff = NULL;
a7b456
+	}
a7b456
       
a7b456
       /* we read zero bytes when pipe closed: this is our signal to exit */ 
a7b456
       if (!read_write(pipefd[0], (unsigned char *)&data, sizeof(data), 1))
a7b456
-- 
a7b456
2.21.1
a7b456