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

Dave Brolley brolley@redhat.com
Wed Jul 13 21:07:00 GMT 2011


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



More information about the Systemtap mailing list