[libvirt] [PATCH 2/7] Coverity: Fix resource leaks in phyp driver

Eric Blake eblake at redhat.com
Thu May 3 22:00:41 UTC 2012


On 05/02/2012 08:51 AM, Osier Yang wrote:
> Coverity logs:
> 
> Error: RESOURCE_LEAK:
> /builddir/build/BUILD/libvirt-0.9.10/src/phyp/phyp_driver.c:523: alloc_fn: Calling allocation function "fopen".
> /builddir/build/BUILD/libvirt-0.9.10/src/phyp/phyp_driver.c:523: var_assign: Assigning: "fd" =  storage returned from "fopen(local_file, "rb")".
> /builddir/build/BUILD/libvirt-0.9.10/src/phyp/phyp_driver.c:540: noescape: Variable "fd" is not freed or pointed-to in function "fread".
> /builddir/build/BUILD/libvirt-0.9.10/src/phyp/phyp_driver.c:542: noescape: Variable "fd" is not freed or pointed-to in function "feof".
> /builddir/build/BUILD/libvirt-0.9.10/src/phyp/phyp_driver.c:575: leaked_storage: Variable "fd" going out of scope leaks the storage it points to.
> /builddir/build/BUILD/libvirt-0.9.10/src/phyp/phyp_driver.c:585: leaked_storage: Variable "fd" going out of scope leaks the storage it points to.
> 

> --- a/src/phyp/phyp_driver.c
> +++ b/src/phyp/phyp_driver.c
> @@ -582,6 +582,7 @@ err:
>          libssh2_channel_free(channel);
>          channel = NULL;
>      }
> +    VIR_FORCE_FCLOSE(fd);
>      return -1;
>  }

ACK if you squash this in (otherwise, you can end up calling
fclose(garbage), never a good idea).  [And why did we name it fd? That's
a lousy name, because it makes me think of low-level fds for open/close,
not high-level files for fopen/fclose.]

diff --git i/src/phyp/phyp_driver.c w/src/phyp/phyp_driver.c
index b883b56..25336ef 100644
--- i/src/phyp/phyp_driver.c
+++ w/src/phyp/phyp_driver.c
@@ -491,7 +491,7 @@ phypUUIDTable_Push(virConnectPtr conn)
     struct stat local_fileinfo;
     char buffer[1024];
     int rc = 0;
-    FILE *fd;
+    FILE *fd = NULL;
     size_t nread, sent;
     char *ptr;
     char local_file[] = "./uuid_table";


-- 
Eric Blake   eblake at redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 620 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120503/16b40355/attachment-0001.sig>


More information about the libvir-list mailing list