qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c
@ 2011-10-31 18:53 Pavel Borzenkov
  2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 1/3] cmd: Fix coding style " Pavel Borzenkov
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Pavel Borzenkov @ 2011-10-31 18:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru

The first patch fixes coding style of the functions affected by next two
patches.
Second patch fixes potential NULL pointer dereference (return value of realloc
is not checked).
Third patch fixes potential memory leak (for the case when realloc returns
NULL).

Pavel Borzenkov (3):
  cmd: Fix coding style in cmd.c
  cmd: Fix potential NULL pointer dereference
  cmd: Fix potential memory leak

 cmd.c |  168 ++++++++++++++++++++++++++++++++---------------------------------
 1 files changed, 82 insertions(+), 86 deletions(-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Qemu-devel] [PATCH v2 1/3] cmd: Fix coding style in cmd.c
  2011-10-31 18:53 [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c Pavel Borzenkov
@ 2011-10-31 18:53 ` Pavel Borzenkov
  2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 2/3] cmd: Fix potential NULL pointer dereference Pavel Borzenkov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Pavel Borzenkov @ 2011-10-31 18:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru

Before the next patches, fix coding style of the affected functions.

Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
---
 cmd.c |  168 ++++++++++++++++++++++++++++++++---------------------------------
 1 files changed, 82 insertions(+), 86 deletions(-)

diff --git a/cmd.c b/cmd.c
index f77897e..a6e3ef4 100644
--- a/cmd.c
+++ b/cmd.c
@@ -45,13 +45,11 @@ compare(const void *a, const void *b)
 		      ((const cmdinfo_t *)b)->name);
 }
 
-void
-add_command(
-	const cmdinfo_t	*ci)
+void add_command(const cmdinfo_t *ci)
 {
-	cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
-	cmdtab[ncmds - 1] = *ci;
-	qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
+    cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
+    cmdtab[ncmds - 1] = *ci;
+    qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
 }
 
 static int
@@ -122,16 +120,15 @@ find_command(
 	return NULL;
 }
 
-void
-add_user_command(char *optarg)
+void add_user_command(char *optarg)
 {
-	ncmdline++;
-	cmdline = realloc(cmdline, sizeof(char*) * (ncmdline));
-	if (!cmdline) {
-		perror("realloc");
-		exit(1);
-	}
-	cmdline[ncmdline-1] = optarg;
+    ncmdline++;
+    cmdline = realloc(cmdline, ncmdline * sizeof(char *));
+    if (!cmdline) {
+        perror("realloc");
+        exit(1);
+    }
+    cmdline[ncmdline-1] = optarg;
 }
 
 static int
@@ -160,45 +157,44 @@ static void prep_fetchline(void *opaque)
 
 static char *get_prompt(void);
 
-void
-command_loop(void)
+void command_loop(void)
 {
-	int		c, i, j = 0, done = 0, fetchable = 0, prompted = 0;
-	char		*input;
-	char		**v;
-	const cmdinfo_t	*ct;
-
-	for (i = 0; !done && i < ncmdline; i++) {
-		input = strdup(cmdline[i]);
-		if (!input) {
-			fprintf(stderr,
-				_("cannot strdup command '%s': %s\n"),
-				cmdline[i], strerror(errno));
-			exit(1);
-		}
-		v = breakline(input, &c);
-		if (c) {
-			ct = find_command(v[0]);
-			if (ct) {
-				if (ct->flags & CMD_FLAG_GLOBAL)
-					done = command(ct, c, v);
-				else {
-					j = 0;
-					while (!done && (j = args_command(j)))
-						done = command(ct, c, v);
-				}
-			} else
-				fprintf(stderr, _("command \"%s\" not found\n"),
-					v[0]);
-		}
-		doneline(input, v);
-	}
-	if (cmdline) {
-		free(cmdline);
-		return;
+    int c, i, j = 0, done = 0, fetchable = 0, prompted = 0;
+    char *input;
+    char **v;
+    const cmdinfo_t *ct;
+
+    for (i = 0; !done && i < ncmdline; i++) {
+        input = strdup(cmdline[i]);
+        if (!input) {
+            fprintf(stderr, _("cannot strdup command '%s': %s\n"),
+                    cmdline[i], strerror(errno));
+            exit(1);
+        }
+        v = breakline(input, &c);
+        if (c) {
+            ct = find_command(v[0]);
+            if (ct) {
+                if (ct->flags & CMD_FLAG_GLOBAL) {
+                    done = command(ct, c, v);
+                } else {
+                    j = 0;
+                    while (!done && (j = args_command(j))) {
+                        done = command(ct, c, v);
+                    }
+                }
+            } else {
+                fprintf(stderr, _("command \"%s\" not found\n"), v[0]);
+            }
 	}
+        doneline(input, v);
+    }
+    if (cmdline) {
+        free(cmdline);
+        return;
+    }
 
-	while (!done) {
+    while (!done) {
         if (!prompted) {
             printf("%s", get_prompt());
             fflush(stdout);
@@ -212,22 +208,24 @@ command_loop(void)
         if (!fetchable) {
             continue;
         }
-		if ((input = fetchline()) == NULL)
-			break;
-		v = breakline(input, &c);
-		if (c) {
-			ct = find_command(v[0]);
-			if (ct)
-				done = command(ct, c, v);
-			else
-				fprintf(stderr, _("command \"%s\" not found\n"),
-					v[0]);
-		}
-		doneline(input, v);
+        input = fetchline();
+        if (input == NULL) {
+            break;
+        }
+        v = breakline(input, &c);
+        if (c) {
+            ct = find_command(v[0]);
+            if (ct) {
+                done = command(ct, c, v);
+            } else {
+                fprintf(stderr, _("command \"%s\" not found\n"), v[0]);
+            }
+        }
+        doneline(input, v);
 
         prompted = 0;
         fetchable = 0;
-	}
+    }
     qemu_aio_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL, NULL, NULL);
 }
 
@@ -331,29 +329,27 @@ static char *qemu_strsep(char **input, const char *delim)
     return result;
 }
 
-char **
-breakline(
-	char	*input,
-	int	*count)
+char **breakline(char *input, int *count)
 {
-	int	c = 0;
-	char	*p;
-	char	**rval = calloc(sizeof(char *), 1);
-
-	while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
-		if (!*p)
-			continue;
-		c++;
-		rval = realloc(rval, sizeof(*rval) * (c + 1));
-		if (!rval) {
-			c = 0;
-			break;
-		}
-		rval[c - 1] = p;
-		rval[c] = NULL;
-	}
-	*count = c;
-	return rval;
+    int c = 0;
+    char *p;
+    char **rval = calloc(sizeof(char *), 1);
+
+    while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
+        if (!*p) {
+            continue;
+        }
+        c++;
+        rval = realloc(rval, sizeof(*rval) * (c + 1));
+        if (!rval) {
+            c = 0;
+            break;
+        }
+        rval[c - 1] = p;
+        rval[c] = NULL;
+    }
+    *count = c;
+    return rval;
 }
 
 void
-- 
1.7.0.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [Qemu-devel] [PATCH v2 2/3] cmd: Fix potential NULL pointer dereference
  2011-10-31 18:53 [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c Pavel Borzenkov
  2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 1/3] cmd: Fix coding style " Pavel Borzenkov
@ 2011-10-31 18:53 ` Pavel Borzenkov
  2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 3/3] cmd: Fix potential memory leak Pavel Borzenkov
  2011-11-01  8:37 ` [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c Stefan Hajnoczi
  3 siblings, 0 replies; 5+ messages in thread
From: Pavel Borzenkov @ 2011-10-31 18:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru

Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
---
 cmd.c |   11 +++--------
 1 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/cmd.c b/cmd.c
index a6e3ef4..75415d8 100644
--- a/cmd.c
+++ b/cmd.c
@@ -47,7 +47,7 @@ compare(const void *a, const void *b)
 
 void add_command(const cmdinfo_t *ci)
 {
-    cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
+    cmdtab = g_realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
     cmdtab[ncmds - 1] = *ci;
     qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
 }
@@ -122,12 +122,7 @@ find_command(
 
 void add_user_command(char *optarg)
 {
-    ncmdline++;
-    cmdline = realloc(cmdline, ncmdline * sizeof(char *));
-    if (!cmdline) {
-        perror("realloc");
-        exit(1);
-    }
+    cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
     cmdline[ncmdline-1] = optarg;
 }
 
@@ -190,7 +185,7 @@ void command_loop(void)
         doneline(input, v);
     }
     if (cmdline) {
-        free(cmdline);
+        g_free(cmdline);
         return;
     }
 
-- 
1.7.0.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [Qemu-devel] [PATCH v2 3/3] cmd: Fix potential memory leak
  2011-10-31 18:53 [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c Pavel Borzenkov
  2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 1/3] cmd: Fix coding style " Pavel Borzenkov
  2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 2/3] cmd: Fix potential NULL pointer dereference Pavel Borzenkov
@ 2011-10-31 18:53 ` Pavel Borzenkov
  2011-11-01  8:37 ` [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c Stefan Hajnoczi
  3 siblings, 0 replies; 5+ messages in thread
From: Pavel Borzenkov @ 2011-10-31 18:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: armbru

Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
---
 cmd.c |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/cmd.c b/cmd.c
index 75415d8..0806e18 100644
--- a/cmd.c
+++ b/cmd.c
@@ -329,16 +329,21 @@ char **breakline(char *input, int *count)
     int c = 0;
     char *p;
     char **rval = calloc(sizeof(char *), 1);
+    char **tmp;
 
     while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
         if (!*p) {
             continue;
         }
         c++;
-        rval = realloc(rval, sizeof(*rval) * (c + 1));
-        if (!rval) {
+        tmp = realloc(rval, sizeof(*rval) * (c + 1));
+        if (!tmp) {
+            free(rval);
+            rval = NULL;
             c = 0;
             break;
+        } else {
+            rval = tmp;
         }
         rval[c - 1] = p;
         rval[c] = NULL;
-- 
1.7.0.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c
  2011-10-31 18:53 [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c Pavel Borzenkov
                   ` (2 preceding siblings ...)
  2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 3/3] cmd: Fix potential memory leak Pavel Borzenkov
@ 2011-11-01  8:37 ` Stefan Hajnoczi
  3 siblings, 0 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2011-11-01  8:37 UTC (permalink / raw)
  To: Pavel Borzenkov; +Cc: qemu-devel, armbru

On Mon, Oct 31, 2011 at 10:53:35PM +0400, Pavel Borzenkov wrote:
> The first patch fixes coding style of the functions affected by next two
> patches.
> Second patch fixes potential NULL pointer dereference (return value of realloc
> is not checked).
> Third patch fixes potential memory leak (for the case when realloc returns
> NULL).
> 
> Pavel Borzenkov (3):
>   cmd: Fix coding style in cmd.c
>   cmd: Fix potential NULL pointer dereference
>   cmd: Fix potential memory leak
> 
>  cmd.c |  168 ++++++++++++++++++++++++++++++++---------------------------------
>  1 files changed, 82 insertions(+), 86 deletions(-)

Thanks, applied to the trivial patches -next tree:

http://repo.or.cz/w/qemu/stefanha.git/shortlog/refs/heads/trivial-patches-next

Stefan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-11-01  9:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-31 18:53 [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c Pavel Borzenkov
2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 1/3] cmd: Fix coding style " Pavel Borzenkov
2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 2/3] cmd: Fix potential NULL pointer dereference Pavel Borzenkov
2011-10-31 18:53 ` [Qemu-devel] [PATCH v2 3/3] cmd: Fix potential memory leak Pavel Borzenkov
2011-11-01  8:37 ` [Qemu-devel] [PATCH v2 0/3] Misc small fixes in cmd.c Stefan Hajnoczi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).