From 4f7cc37e5f4a209773062d67e5f61a81f7800fc8 Mon Sep 17 00:00:00 2001 From: Richard Jones Date: Thu, 30 Apr 2009 13:36:06 +0100 Subject: [PATCH] Fix unchecked malloc & realloc in 'commandv' func (Jim Meyering). --- daemon/guestfsd.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index d7ba482..ba4035e 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -339,20 +339,31 @@ int command (char **stdoutput, char **stderror, const char *name, ...) { va_list args; - char **argv; + char **argv, **p; char *s; int i, r; /* Collect the command line arguments into an array. */ - va_start (args, name); - i = 2; argv = malloc (sizeof (char *) * i); + if (argv == NULL) { + perror ("malloc"); + return -1; + } argv[0] = (char *) name; argv[1] = NULL; + va_start (args, name); + while ((s = va_arg (args, char *)) != NULL) { - argv = realloc (argv, sizeof (char *) * (++i)); + p = realloc (argv, sizeof (char *) * (++i)); + if (p == NULL) { + perror ("realloc"); + free (argv); + va_end (args); + return -1; + } + argv = p; argv[i-2] = s; argv[i-1] = NULL; } -- 1.8.3.1