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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxx    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]