[libvirt] [PATCH] node_device_linux_sysfs.c: avoid opendir/fd leak on error path

Jim Meyering jim at meyering.net
Wed Jan 20 20:58:52 UTC 2010


Dave Allan wrote:
...
>>       closedir(dir);
>> +    dir = NULL;
>>
>>       ret = 0;
>>
>>   out:
>> +    if (dir)
>> +        closedir(dir);
>>       VIR_FREE(device_link);
>>       return 0;
>>   }
...
> Since dir is initialized to NULL, you could also simplify things by
> removing the closedir/dir = NULL statements above the out:
> label. Either way, this version looks safe to me.

Good idea.
I've just done that, too:

commit 21c84e00d3c094abc5e429eec83c6fb5b2e25695
Author: Jim Meyering <meyering at redhat.com>
Date:   Wed Jan 20 21:57:34 2010 +0100

    clean-up: remove unnecessary closedir call

    * src/node_device/node_device_linux_sysfs.c (get_virtual_functions_linux):
    Remove unnecessary closedir.  Spotted by Dave Allan.

diff --git a/src/node_device/node_device_linux_sysfs.c b/src/node_device/node_device_linux_sysfs.c
index 674ee26..73d8f8e 100644
--- a/src/node_device/node_device_linux_sysfs.c
+++ b/src/node_device/node_device_linux_sysfs.c
@@ -362,17 +362,14 @@ int get_virtual_functions_linux(const char *sysfs_path,
                 d->pci_dev.flags |= VIR_NODE_DEV_CAP_FLAG_PCI_VIRTUAL_FUNCTION;
             }

             VIR_FREE(device_link);
         }
     }

-    closedir(dir);
-    dir = NULL;
-
     ret = 0;

 out:
     if (dir)
         closedir(dir);
     VIR_FREE(device_link);
     return 0;




More information about the libvir-list mailing list