[libvirt] [PATCH 2/2] build: fix getcwd portability problems
Eric Blake
eblake at redhat.com
Fri Apr 29 18:13:44 UTC 2011
On 04/29/2011 11:48 AM, Matthias Bolte wrote:
>> +++ 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.
It's technically only a leak if someone overwrites abs_srcdir with
different contents without freeing it first, since there is a global
variable that still tracks the pointer through the point of program
exit(). Valgrind reports this type of open-ended allocation as "still
reachable", rather than "definitely lost".
But, to make valgrind even quieter, yes, I can fix things up to free the
memory if it was not read from getenv and before returning from
virtTestMain.
>
> ACK, with that memory leak fixed.
Pushed with this addition:
diff --git i/tests/testutils.c w/tests/testutils.c
index 91035a2..ae73530 100644
--- i/tests/testutils.c
+++ w/tests/testutils.c
@@ -478,6 +478,7 @@ int virtTestMain(int argc,
int (*func)(void))
{
int ret;
+ bool abs_srcdir_cleanup = falseb;
#if TEST_OOM
int approxAlloc = 0;
int n;
@@ -489,8 +490,10 @@ int virtTestMain(int argc,
#endif
abs_srcdir = getenv("abs_srcdir");
- if (!abs_srcdir)
+ if (!abs_srcdir) {
abs_srcdir = getcwd(NULL, 0);
+ abs_srcdir_cleanup = true;
+ }
if (!abs_srcdir)
exit(EXIT_AM_HARDFAIL);
@@ -623,6 +626,8 @@ cleanup:
ret = (func)();
#endif
+ if (abs_srcdir_cleanup)
+ VIR_FREE(abs_srcdir);
virResetLastError();
if (!virTestGetVerbose()) {
int i;
--
Eric Blake eblake at redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20110429/0e441d19/attachment-0001.sig>
More information about the libvir-list
mailing list