* [PATCH] stap-serverd.cxx: fix memory and resource leaks
@ 2011-07-13 16:42 Petr Muller
2011-07-13 21:07 ` Dave Brolley
0 siblings, 1 reply; 2+ messages in thread
From: Petr Muller @ 2011-07-13 16:42 UTC (permalink / raw)
To: systemtap
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] stap-serverd.cxx: fix memory and resource leaks
2011-07-13 16:42 [PATCH] stap-serverd.cxx: fix memory and resource leaks Petr Muller
@ 2011-07-13 21:07 ` Dave Brolley
0 siblings, 0 replies; 2+ messages in thread
From: Dave Brolley @ 2011-07-13 21:07 UTC (permalink / raw)
To: Petr Muller; +Cc: systemtap
Thanks! commit 1198c36dac639a72a883f886eeb6f0c637c56b10
Dave
On 07/13/2011 12:41 PM, Petr Muller wrote:
> 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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-07-13 21:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-13 16:42 [PATCH] stap-serverd.cxx: fix memory and resource leaks Petr Muller
2011-07-13 21:07 ` Dave Brolley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).