Skip to contentSkip to conversation

exec: fix error handling in file_ram_alloc

PATCH In review Paolo Bonzini 2 participants 2 messages Mar 17, 2016 – Mar 18, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 exec: fix error handling in file_ram_alloc

One instance of double closing, and invalid close(-1) in some cases of "goto error".

---
 exec.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
exec.c +4 -3
index f398d21..5c16f41 100644
@@ -1238,7 +1238,7 @@ static void *file_ram_alloc(RAMBlock *block,
     char *sanitized_name;
     char *c;
     void *area;
-    int fd;
+    int fd = -1;
     int64_t page_size;
 
     if (kvm_enabled() && !kvm_has_sync_mmu()) {
@@ -1320,7 +1320,6 @@ static void *file_ram_alloc(RAMBlock *block,
     if (area == MAP_FAILED) {
         error_setg_errno(errp, errno,
                          "unable to map backing store for guest RAM");
-        close(fd);
         goto error;
     }
 
@@ -1335,7 +1334,9 @@ error:
     if (unlink_on_error) {
         unlink(path);
     }
-    close(fd);
+    if (fd != -1) {
+        close(fd);
+    }
     return NULL;
 }
 #endif
Signature · 1 line
2.5.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 31 lines · Paolo Bonzini

One instance of double closing, and invalid close(-1) in some cases of "goto error".

---
 exec.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
exec.c +1 -2
index f398d21..5c16f41 100644
@@ -1238,7 +1238,7 @@ static void *file_ram_alloc(RAMBlock *block,
     char *sanitized_name;
     char *c;
     void *area;
-    int fd;
+    int fd = -1;
     int64_t page_size;
 
     if (kvm_enabled() && !kvm_has_sync_mmu()) {
@@ -1320,7 +1320,6 @@ static void *file_ram_alloc(RAMBlock *block,
     if (area == MAP_FAILED) {
         error_setg_errno(errp, errno,
                          "unable to map backing store for guest RAM");
-        close(fd);
         goto error;
     }
 

exec.c error: · line 1342

Quoted 10 lines
exec.c +3 -1
@@ -1335,7 +1334,9 @@ error:
     if (unlink_on_error) {
         unlink(path);
     }
-    close(fd);
+    if (fd != -1) {
+        close(fd);
+    }
     return NULL;
 }
 #endif