]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
storage: avoid memory leak on stat failure
authorEric Blake <eblake@redhat.com>
Thu, 2 Jun 2011 22:27:48 +0000 (16:27 -0600)
committerEric Blake <eblake@redhat.com>
Fri, 3 Jun 2011 14:11:42 +0000 (08:11 -0600)
Spotted by coverity.  Triggers on failed stat, although I'm not sure
how easy that condition is, so I'm not sure if this is a runtime
memory hog.  Regression introduced in commit 8077d64 (unreleased).

* src/util/storage_file.c (virStorageFileGetMetadataFromFD):
Reduce need for malloc, avoiding a leak.

src/util/storage_file.c

index 8dbd9335cb77b48ba3c1d577aa495cacc961f4ce..6b3b756362ce5d916678c2b17453d58b708f3ffc 100644 (file)
@@ -831,11 +831,6 @@ virStorageFileGetMetadataFromFD(const char *path,
     int ret = -1;
     struct stat sb;
 
-    if (VIR_ALLOC_N(head, len) < 0) {
-        virReportOOMError();
-        return -1;
-    }
-
     memset(meta, 0, sizeof (*meta));
 
     if (fstat(fd, &sb) < 0) {
@@ -847,13 +842,17 @@ virStorageFileGetMetadataFromFD(const char *path,
 
     /* No header to probe for directories */
     if (S_ISDIR(sb.st_mode)) {
-        ret = 0;
-        goto cleanup;
+        return 0;
     }
 
     if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
         virReportSystemError(errno, _("cannot seek to start of '%s'"), path);
-        goto cleanup;
+        return -1;
+    }
+
+    if (VIR_ALLOC_N(head, len) < 0) {
+        virReportOOMError();
+        return -1;
     }
 
     if ((len = read(fd, head, len)) < 0) {