[icinga-checkins] icinga.org: icinga2/master: Fixed memory leak when using execvpe() with vfork().

git at icinga.org git at icinga.org
Mon Feb 11 14:42:23 CET 2013


Module: icinga2
Branch: master
Commit: 958a05087439745bbf713f1c0542a8685e8021a3
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=958a05087439745bbf713f1c0542a8685e8021a3

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Mon Feb 11 14:42:08 2013 +0100

Fixed memory leak when using execvpe() with vfork().

---

 third-party/execvpe/execvpe.c |   21 ++++++++++-----------
 1 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/third-party/execvpe/execvpe.c b/third-party/execvpe/execvpe.c
index 6ce1e9d..a896387 100644
--- a/third-party/execvpe/execvpe.c
+++ b/third-party/execvpe/execvpe.c
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <alloca.h>
 
 /*
  * We want the search semantics of execvp, but we want to provide our
@@ -77,20 +78,23 @@ execvpe(char *name, char *const argv[], char **envp)
     if (!(path = getenv("PATH"))) {
 #ifdef HAVE_CONFSTR
         ln = confstr(_CS_PATH, NULL, 0);
-        if ((cur = path = malloc(ln + 1)) != NULL) {
+        if ((cur = path = alloca(ln + 1)) != NULL) {
 	    path[0] = ':';
 	    (void) confstr (_CS_PATH, path + 1, ln);
 	}
 #else
-        if ((cur = path = malloc(1 + 1)) != NULL) {
+        if ((cur = path = alloca(1 + 1)) != NULL) {
 	    path[0] = ':';
 	    path[1] = '\0';
 	}
 #endif
-    } else
-	cur = path = strdup(path);
+    } else {
+        cur = alloca(strlen(path) + 1);
+        strdup(cur, path);
+        path = cur;
+    }
 
-    if (path == NULL || (bp = buf = malloc(strlen(path)+strlen(name)+2)) == NULL)
+    if (path == NULL || (bp = buf = alloca(strlen(path)+strlen(name)+2)) == NULL)
 	goto done;
 
     while (cur != NULL) {
@@ -129,13 +133,12 @@ execvpe(char *name, char *const argv[], char **envp)
 
 		for (cnt = 0, ap = (char **) argv; *ap; ++ap, ++cnt)
 		    ;
-		if ((ap = malloc((cnt + 2) * sizeof(char *))) != NULL) {
+		if ((ap = alloca((cnt + 2) * sizeof(char *))) != NULL) {
 		    memcpy(ap + 2, argv + 1, cnt * sizeof(char *));
 
 		    ap[0] = "sh";
 		    ap[1] = bp;
 		    (void) execve("/bin/sh", ap, envp);
-		    free(ap);
 		}
 		goto done;
 	    }
@@ -152,10 +155,6 @@ execvpe(char *name, char *const argv[], char **envp)
     else if (!errno)
 	errno = ENOENT;
   done:
-    if (path)
-	free(path);
-    if (buf)
-	free(buf);
     return (-1);
 }
 #endif





More information about the icinga-checkins mailing list