[libvirt] [PATCH 2/2] build: fix getcwd portability problems

Matthias Bolte matthias.bolte at googlemail.com
Fri Apr 29 17:48:27 UTC 2011


2011/4/29 Eric Blake <eblake at redhat.com>:
> * bootstrap.conf (gnulib_modules): Add getcwd-lgpl.
> * tests/commandtest.c (checkoutput): Drop unused cwd.
> * tests/commandhelper.c (main): Let getcwd malloc.
> * tests/testutils.c (virTestMain): Likewise.
> * tools/virsh.c (cmdPwd): Likewise.
> (virshCmds): Expose cmdPwd and cmdCd on mingw.
> ---
>
>> Oh, and now that gnulib getcwd-lgpl exists, we could use it
>> to fix our uses of getcwd(NULL,0), followup coming later.
>
>  bootstrap.conf        |    1 +
>  tests/commandhelper.c |    7 ++++---
>  tests/commandtest.c   |    4 ----
>  tests/testutils.c     |    3 +--
>  tools/virsh.c         |   34 ++++++++--------------------------
>  5 files changed, 14 insertions(+), 35 deletions(-)


> diff --git a/tests/testutils.c b/tests/testutils.c
> index 456a735..91035a2 100644
> --- a/tests/testutils.c
> +++ b/tests/testutils.c
> @@ -478,7 +478,6 @@ int virtTestMain(int argc,
>                  int (*func)(void))
>  {
>     int ret;
> -    char cwd[PATH_MAX];
>  #if TEST_OOM
>     int approxAlloc = 0;
>     int n;
> @@ -491,7 +490,7 @@ int virtTestMain(int argc,
>
>     abs_srcdir = getenv("abs_srcdir");
>     if (!abs_srcdir)
> -        abs_srcdir = getcwd(cwd, sizeof(cwd));
> +        abs_srcdir = getcwd(NULL, 0);
>     if (!abs_srcdir)
>         exit(EXIT_AM_HARDFAIL);

Now you have created a memory leak (not a critical one, that's true),
because abs_srcdir can be malloc'ed and you missed to free it.

ACK, with that memory leak fixed.

Matthias




More information about the libvir-list mailing list