Skip to contentSkip to conversation

Fix error handling in qemu_read_config_file

PATCH Applied Kevin Wolf 2 participants 2 messages May 17, 2010 – May 24, 2010

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 Fix error handling in qemu_read_config_file

We need to close the file even in error case. While at it, make the callers catch all kind of errors. ENOENT is allowed for default config files, they are optional.

---
 qemu-config.c |   12 ++++++++----
 vl.c          |    4 ++--
 2 files changed, 10 insertions(+), 6 deletions(-)
qemu-config.c +8 -4
index bf3d493..b2a4128 100644
@@ -521,14 +521,18 @@ out:
 int qemu_read_config_file(const char *filename)
 {
     FILE *f = fopen(filename, "r");
+    int ret;
+
     if (f == NULL) {
         return -errno;
     }
 
-    if (qemu_config_parse(f, vm_config_groups, filename) != 0) {
-        return -EINVAL;
-    }
+    ret = qemu_config_parse(f, vm_config_groups, filename);
     fclose(f);
 
-    return 0;
+    if (ret == 0) {
+        return 0;
+    } else {
+        return -EINVAL;
+    }
 }
vl.c +2 -2
index c8abce6..4ca1bee 100644
@@ -2683,12 +2683,12 @@ int main(int argc, char **argv, char **envp)
         int ret;
 
         ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
-        if (ret == -EINVAL) {
+        if (ret < 0 && ret != -ENOENT) {
             exit(1);
         }
 
         ret = qemu_read_config_file(arch_config_name);
-        if (ret == -EINVAL) {
+        if (ret < 0 && ret != -ENOENT) {
             exit(1);
         }
     }
Signature · 1 line
1.6.6.1
#1 lore raw
Quoted 7 lines · Kevin Wolf

We need to close the file even in error case. While at it, make the callers catch all kind of errors. ENOENT is allowed for default config files, they are optional.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 52 lines
---
  qemu-config.c |   12 ++++++++----
  vl.c          |    4 ++--
  2 files changed, 10 insertions(+), 6 deletions(-)
qemu-config.c +8 -4
index bf3d493..b2a4128 100644
@@ -521,14 +521,18 @@ out:
  int qemu_read_config_file(const char *filename)
  {
      FILE *f = fopen(filename, "r");
+    int ret;
+
      if (f == NULL) {
          return -errno;
      }
 
-    if (qemu_config_parse(f, vm_config_groups, filename) != 0) {
-        return -EINVAL;
-    }
+    ret = qemu_config_parse(f, vm_config_groups, filename);
      fclose(f);
 
-    return 0;
+    if (ret == 0) {
+        return 0;
+    } else {
+        return -EINVAL;
+    }
  }
vl.c +2 -2
index c8abce6..4ca1bee 100644
@@ -2683,12 +2683,12 @@ int main(int argc, char **argv, char **envp)
          int ret;
 
          ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
-        if (ret == -EINVAL) {
+        if (ret<  0&&  ret != -ENOENT) {
              exit(1);
          }
 
          ret = qemu_read_config_file(arch_config_name);
-        if (ret == -EINVAL) {
+        if (ret<  0&&  ret != -ENOENT) {
              exit(1);
          }
      }