Patchwork cmd: Fix potential NULL pointer dereference

login
register
mail settings
Submitter Pavel Borzenkov
Date Oct. 28, 2011, 9:48 p.m.
Message ID <1319838533-25405-1-git-send-email-pavel.borzenkov@gmail.com>
Download mbox | patch
Permalink /patch/122499/
State New
Headers show

Comments

Pavel Borzenkov - Oct. 28, 2011, 9:48 p.m.
Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
---
Note, that almost all code in the file violates CODING STYLE. The changed lines
are written wrt CODING STYLE (like commits 7d7d975c and c32d766a).
---

 cmd.c |   11 +++--------
 1 files changed, 3 insertions(+), 8 deletions(-)
Markus Armbruster - Oct. 31, 2011, 6:27 a.m.
Pavel Borzenkov <pavel.borzenkov@gmail.com> writes:

> Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
> ---
> Note, that almost all code in the file violates CODING STYLE. The changed lines
> are written wrt CODING STYLE (like commits 7d7d975c and c32d766a).
>
> ---
>
>  cmd.c |   11 +++--------
>  1 files changed, 3 insertions(+), 8 deletions(-)
>
> diff --git a/cmd.c b/cmd.c
> index f77897e..abcf206 100644
> --- a/cmd.c
> +++ b/cmd.c
> @@ -49,7 +49,7 @@ 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);
>  }

Inconsistent indentation.

Either stick to the original indentation, or reindent the whole
function.

[More of the same...]
Pavel Borzenkov - Oct. 31, 2011, 7:18 a.m.
On Mon, Oct 31, 2011 at 10:27 AM, Markus Armbruster <armbru@redhat.com> wrote:
> Pavel Borzenkov <pavel.borzenkov@gmail.com> writes:
>
>> Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
>> ---
>> Note, that almost all code in the file violates CODING STYLE. The changed lines
>> are written wrt CODING STYLE (like commits 7d7d975c and c32d766a).
>>
>> ---
>>
>>  cmd.c |   11 +++--------
>>  1 files changed, 3 insertions(+), 8 deletions(-)
>>
>> diff --git a/cmd.c b/cmd.c
>> index f77897e..abcf206 100644
>> --- a/cmd.c
>> +++ b/cmd.c
>> @@ -49,7 +49,7 @@ 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);
>>  }
>
> Inconsistent indentation.
>
> Either stick to the original indentation, or reindent the whole
> function.

I'll send second version with coding style fixes made in a separate commit.

Patch

diff --git a/cmd.c b/cmd.c
index f77897e..abcf206 100644
--- a/cmd.c
+++ b/cmd.c
@@ -49,7 +49,7 @@  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);
 }
@@ -125,12 +125,7 @@  find_command(
 void
 add_user_command(char *optarg)
 {
-	ncmdline++;
-	cmdline = realloc(cmdline, sizeof(char*) * (ncmdline));
-	if (!cmdline) {
-		perror("realloc");
-		exit(1);
-	}
+    cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
 	cmdline[ncmdline-1] = optarg;
 }
 
@@ -194,7 +189,7 @@  command_loop(void)
 		doneline(input, v);
 	}
 	if (cmdline) {
-		free(cmdline);
+        g_free(cmdline);
 		return;
 	}