[Libguestfs] [PATCH 06/13] fish: edit: improve the editor execution

Pino Toscano ptoscano at redhat.com
Thu Aug 28 13:21:09 UTC 2014


Dynamically allocate the buffer for the command, and check also its exit
status.

Improvements taken from the current implementation in virt-edit.
---
 fish/file-edit.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/fish/file-edit.c b/fish/file-edit.c
index ec707a9..408d20e 100644
--- a/fish/file-edit.c
+++ b/fish/file-edit.c
@@ -43,6 +43,7 @@ edit_file_editor (guestfs_h *g, const char *filename, const char *editor)
   CLEANUP_UNLINK_FREE char *tmpfilename = NULL;
   char buf[256];
   CLEANUP_FREE char *newname = NULL;
+  CLEANUP_FREE char *cmd = NULL;
   struct stat oldstat, newstat;
   int r, fd;
 
@@ -77,12 +78,14 @@ edit_file_editor (guestfs_h *g, const char *filename, const char *editor)
   }
 
   /* Edit it. */
-  /* XXX Safe? */
-  snprintf (buf, sizeof buf, "%s %s", editor, filename);
+  if (asprintf (&cmd, "%s %s", editor, tmpfilename) == -1) {
+    perror ("asprintf");
+    return -1;
+  }
 
-  r = system (buf);
-  if (r != 0) {
-    perror (buf);
+  r = system (cmd);
+  if (r == -1 || WEXITSTATUS (r) != 0) {
+    perror (cmd);
     return -1;
   }
 
-- 
1.9.3




More information about the Libguestfs mailing list