]> xenbits.xensource.com Git - libvirt.git/commitdiff
openvzGetVEID: don't leak (memory + file descriptor)
authorJim Meyering <meyering@redhat.com>
Thu, 25 Feb 2010 18:24:50 +0000 (19:24 +0100)
committerJim Meyering <meyering@redhat.com>
Thu, 25 Feb 2010 18:28:59 +0000 (19:28 +0100)
* src/openvz/openvz_conf.c (openvzGetVEID): Always call fclose.
Diagnose parse failure also when vzlist output is empty.
If somehow we read a -1, diagnose that (albeit as a parse failure).

src/openvz/openvz_conf.c

index ce0c4d332c9021cfa843a2d06eff3c6a239ae06c..3713a458df9235abcc6e66830f7652448c835e67 100644 (file)
@@ -964,6 +964,7 @@ int openvzGetVEID(const char *name) {
     char *cmd;
     int veid;
     FILE *fp;
+    bool ok;
 
     if (virAsprintf(&cmd, "%s %s -ovpsid -H", VZLIST, name) < 0) {
         openvzError(NULL, VIR_ERR_INTERNAL_ERROR, "%s",
@@ -979,18 +980,12 @@ int openvzGetVEID(const char *name) {
         return -1;
     }
 
-    if (fscanf(fp, "%d\n", &veid ) != 1) {
-        if (feof(fp))
-            return -1;
-
-        openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
-                    "%s", _("Failed to parse vzlist output"));
-        goto cleanup;
-    }
-
-    return veid;
-
- cleanup:
+    ok = fscanf(fp, "%d\n", &veid ) == 1;
     fclose(fp);
+    if (ok && veid >= 0)
+        return veid;
+
+    openvzError(NULL, VIR_ERR_INTERNAL_ERROR,
+                "%s", _("Failed to parse vzlist output"));
     return -1;
 }