--- p2v/miniexpect.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/p2v/miniexpect.c b/p2v/miniexpect.c index ec81030..1baab5f 100644 --- a/p2v/miniexpect.c +++ b/p2v/miniexpect.c @@ -114,6 +114,7 @@ mexp_spawnl (const char *file, const char *arg, ...) new_argv = realloc (argv, sizeof (char *) * (i+1)); if (new_argv == NULL) { free (argv); + va_end (args); return NULL; } argv = new_argv; @@ -122,6 +123,7 @@ mexp_spawnl (const char *file, const char *arg, ...) h = mexp_spawnv (file, argv); free (argv); + va_end (args); return h; } -- 1.9.3
Pino Toscano
2014-Jul-25 15:36 UTC
[Libguestfs] [PATCH] cleanups: add CLEANUP_FCLOSE (fclose on FILE*)
--- src/cleanup.c | 9 +++++++++ src/guestfs-internal-frontend.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/src/cleanup.c b/src/cleanup.c index 86bb547..4e1a7f9 100644 --- a/src/cleanup.c +++ b/src/cleanup.c @@ -122,3 +122,12 @@ guestfs___cleanup_xmlXPathFreeObject (void *ptr) if (obj) xmlXPathFreeObject (obj); } + +void +guestfs___cleanup_fclose (void *ptr) +{ + FILE *f = * (FILE **) ptr; + + if (f) + fclose (f); +} diff --git a/src/guestfs-internal-frontend.h b/src/guestfs-internal-frontend.h index 6bf0a94..acd7be5 100644 --- a/src/guestfs-internal-frontend.h +++ b/src/guestfs-internal-frontend.h @@ -55,6 +55,7 @@ __attribute__((cleanup(guestfs___cleanup_xmlXPathFreeContext))) #define CLEANUP_XMLXPATHFREEOBJECT \ __attribute__((cleanup(guestfs___cleanup_xmlXPathFreeObject))) +#define CLEANUP_FCLOSE __attribute__((cleanup(guestfs___cleanup_fclose))) #else #define CLEANUP_FREE #define CLEANUP_FREE_STRING_LIST @@ -66,6 +67,7 @@ #define CLEANUP_XMLFREETEXTWRITER #define CLEANUP_XMLXPATHFREECONTEXT #define CLEANUP_XMLXPATHFREEOBJECT +#define CLEANUP_FCLOSE #endif /* NB: At some point we will stop exporting these safe_* allocation @@ -115,6 +117,7 @@ extern void guestfs___cleanup_xmlFreeURI (void *ptr); extern void guestfs___cleanup_xmlFreeTextWriter (void *ptr); extern void guestfs___cleanup_xmlXPathFreeContext (void *ptr); extern void guestfs___cleanup_xmlXPathFreeObject (void *ptr); +extern void guestfs___cleanup_fclose (void *ptr); /* These are in a separate header so the header can be generated. * Don't include the following file directly: -- 1.9.3
--- make-fs/make-fs.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/make-fs/make-fs.c b/make-fs/make-fs.c index a03bb8f..1bec3e2 100644 --- a/make-fs/make-fs.c +++ b/make-fs/make-fs.c @@ -378,7 +378,7 @@ estimate_input (const char *input, uint64_t *estimate_rtn, char **ifmt_rtn) struct stat statbuf; const char *argv[6]; CLEANUP_UNLINK_FREE char *tmpfile = NULL; - FILE *fp; + CLEANUP_FCLOSE FILE *fp = NULL; char line[256]; size_t len; @@ -419,10 +419,8 @@ estimate_input (const char *input, uint64_t *estimate_rtn, char **ifmt_rtn) } if (fgets (line, sizeof line, fp) == NULL) { perror ("fgets"); - fclose (fp); return -1; } - fclose (fp); if (sscanf (line, "%" SCNu64, estimate_rtn) != 1) { fprintf (stderr, _("%s: cannot parse the output of 'du' command: %s\n"), @@ -448,7 +446,6 @@ estimate_input (const char *input, uint64_t *estimate_rtn, char **ifmt_rtn) perror ("fgets"); return -1; } - fclose (fp); len = strlen (line); if (len > 0 && line[len-1] == '\n') -- 1.9.3
Pino Toscano
2014-Jul-25 15:36 UTC
[Libguestfs] [PATCH] p2v: close the mexp handle if mexp_spawnv fails
--- p2v/miniexpect.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/p2v/miniexpect.c b/p2v/miniexpect.c index 1baab5f..e1a184c 100644 --- a/p2v/miniexpect.c +++ b/p2v/miniexpect.c @@ -130,7 +130,7 @@ mexp_spawnl (const char *file, const char *arg, ...) mexp_h * mexp_spawnv (const char *file, char **argv) { - mexp_h *h; + mexp_h *h = NULL; int fd = -1; int err; char slave[1024]; @@ -206,6 +206,8 @@ mexp_spawnv (const char *file, char **argv) close (fd); if (pid > 0) waitpid (pid, NULL, 0); + if (h != NULL) + mexp_close (h); errno = err; return NULL; } -- 1.9.3
Richard W.M. Jones
2014-Jul-26 12:43 UTC
Re: [Libguestfs] [PATCH] p2v: properly call va_end
On Fri, Jul 25, 2014 at 05:36:14PM +0200, Pino Toscano wrote:> --- > p2v/miniexpect.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/p2v/miniexpect.c b/p2v/miniexpect.c > index ec81030..1baab5f 100644 > --- a/p2v/miniexpect.c > +++ b/p2v/miniexpect.c > @@ -114,6 +114,7 @@ mexp_spawnl (const char *file, const char *arg, ...) > new_argv = realloc (argv, sizeof (char *) * (i+1)); > if (new_argv == NULL) { > free (argv); > + va_end (args); > return NULL; > } > argv = new_argv; > @@ -122,6 +123,7 @@ mexp_spawnl (const char *file, const char *arg, ...) > > h = mexp_spawnv (file, argv); > free (argv); > + va_end (args); > return h; > }Ugh yes. Coverity, right? Please push obvious fixes found by Coverity like these. They don't need acking I don't think. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org