Misc small fixes in cmd.c
v2 cover Misc small fixes in cmd.c
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(-)
PATCH v2 1/3 cmd: Fix coding style in cmd.c
Before the next patches, fix coding style of the affected functions.
--- cmd.c | 168 ++++++++++++++++++++++++++++++++--------------------------------- 1 files changed, 82 insertions(+), 86 deletions(-)
cmd.c +82 -86
@@ -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
Signature · 1 line
1.7.0.4
PATCH v2 2/3 cmd: Fix potential NULL pointer dereference
In reply to Pavel Borzenkov--- cmd.c | 11 +++-------- 1 files changed, 3 insertions(+), 8 deletions(-)
cmd.c +3 -8
@@ -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 ; }
Signature · 1 line
1.7.0.4
PATCH v2 3/3 cmd: Fix potential memory leak
In reply to Pavel Borzenkov--- cmd.c | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-)
cmd.c +7 -2
@@ -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 ;
Signature · 1 line
1.7.0.4
Stefan Hajnoczi
AppliedQuoted 7 lines · Pavel Borzenkov
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).
Quoted 7 lines
Pavel Borzenkov (3):
cmd: Fix coding style incmd.c
cmd: Fix potential NULL pointer dereference
cmd: Fix potential memory leakcmd.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