This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] stap-serverd.cxx: fix memory and resource leaks


While playing with cppcheck tool, I found few resource leaks in stap-serverd.cxx:
- handleRequest: arg was not freed if opening/reading argfile failed
- handleRequest: argfile was not fclosed when reading from it failed
- spawn_and_wait: dotfd was not closed if chdir fails (macro expanded to accomodate resource release)
- spawn_and_wait: cleaned some whitespace up around the fix itself
---
 stap-serverd.cxx |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/stap-serverd.cxx b/stap-serverd.cxx
index 747e177..25e05ef 100644
--- a/stap-serverd.cxx
+++ b/stap-serverd.cxx
@@ -1097,6 +1097,7 @@ handleRequest (const string &requestDirName, const string &responseDirName)
       argfile = fopen(stapargfile, "r");
       if (! argfile)
         {
+          free(arg);
           server_error (_F("Error opening %s: %s", stapargfile, strerror (errno)));
           return;
         }
@@ -1104,6 +1105,8 @@ handleRequest (const string &requestDirName, const string &responseDirName)
       rc = fread(arg, 1, st.st_size, argfile);
       if (rc != st.st_size)
         {
+          free(arg);
+          fclose(argfile);
           server_error (_F("Error reading %s: %s", stapargfile, strerror (errno)));
           return;
         }
@@ -1250,15 +1253,20 @@ spawn_and_wait (const vector<string> &argv,
     {
       dotfd = open (".", O_RDONLY);
       if (dotfd < 0)
-        { 
+        {
           server_error (_("Error in spawn getcwd"));
           return PR_FAILURE;
         }
-      
+
       rc = chdir (pwd);
-      CHECKRC ("Error in spawn chdir");
-    } 
- 
+      if (rc)
+        {
+          close(dotfd);
+          server_error(_("Error in spawn chdir"));
+          return PR_FAILURE;
+        }
+    }
+
   // Set resource limits, if requested, in order to prevent
   // DOS. spawn_and_wait ultimately uses posix_spawp which behaves like
   // fork (according to the posix_spawnbp man page), so the limits we set here will be
-- 
1.7.6



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]