From: Guus Sliepen <guus@tinc-vpn.org>
Date: Wed, 14 Feb 2007 09:21:34 +0000 (+0000)
Subject: Apply patch from Scott Lamb fixing some memory and resource leaks.
X-Git-Tag: release-1.0.8~6
X-Git-Url: http://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=45fca3c723302868de3225e7509d2292008948f7;p=tinc

Apply patch from Scott Lamb fixing some memory and resource leaks.
---

diff --git a/lib/pidfile.c b/lib/pidfile.c
index 61a802f6..08d96dfe 100644
--- a/lib/pidfile.c
+++ b/lib/pidfile.c
@@ -84,8 +84,13 @@ pid_t write_pid (char *pidfile)
   int fd;
   pid_t pid;
 
-  if ( ((fd = open(pidfile, O_RDWR|O_CREAT, 0644)) == -1)
-       || ((f = fdopen(fd, "r+")) == NULL) ) {
+  if ((fd = open(pidfile, O_RDWR|O_CREAT, 0644)) == -1) {
+      close(fd);
+      return 0;
+  }
+
+  if ((f = fdopen(fd, "r+")) == NULL) {
+      fclose(f);
       return 0;
   }
   
@@ -98,18 +103,18 @@ pid_t write_pid (char *pidfile)
 
   pid = getpid();
   if (!fprintf(f,"%ld\n", (long)pid)) {
-      close(fd);
+      fclose(f);
       return 0;
   }
   fflush(f);
 
 #ifdef HAVE_FLOCK
   if (flock(fd, LOCK_UN) == -1) {
-      close(fd);
+      fclose(f);
       return 0;
   }
 #endif
-  close(fd);
+  fclose(f);
 
   return pid;
 }
diff --git a/src/process.c b/src/process.c
index 11e8b4fc..30ff82ad 100644
--- a/src/process.c
+++ b/src/process.c
@@ -379,8 +379,10 @@ bool execute_script(const char *name, char **envp)
 
 	/* First check if there is a script */
 
-	if(stat(scriptname + 1, &s))
+	if(stat(scriptname + 1, &s)) {
+		free(scriptname);
 		return true;
+	}
 
 	ifdebug(STATUS) logger(LOG_INFO, _("Executing script %s"), name);