Explicitly returns NULL if asprintf() fails, since the asprintf(3)
man-page says that the contents of the first argument are undefined if
any error occurs.
---
src/shared/shell.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/src/shared/shell.c b/src/shared/shell.c
index 0a05b5215..7417e7ab4 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -643,8 +643,13 @@ static char *cmd_generator(const char *text, int state)
}
cmd = find_cmd(text + strlen(menu->name) + 1, menu->entries, &index);
- if (cmd)
- asprintf(&cmd, "%s.%s", menu->name, cmd);
+ if (cmd) {
+ int err;
+
+ err = asprintf(&cmd, "%s.%s", menu->name, cmd);
+ if (err < 0)
+ return NULL;
+ }
return cmd;
}
--
2.14.1
Hi Eramoto,
On Mon, Mar 19, 2018 at 6:46 AM, ERAMOTO Masaya
<[email protected]> wrote:
> Explicitly returns NULL if asprintf() fails, since the asprintf(3)
> man-page says that the contents of the first argument are undefined if
> any error occurs.
> ---
> src/shared/shell.c | 9 +++++++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/src/shared/shell.c b/src/shared/shell.c
> index 0a05b5215..7417e7ab4 100644
> --- a/src/shared/shell.c
> +++ b/src/shared/shell.c
> @@ -643,8 +643,13 @@ static char *cmd_generator(const char *text, int state)
> }
>
> cmd = find_cmd(text + strlen(menu->name) + 1, menu->entries, &index);
> - if (cmd)
> - asprintf(&cmd, "%s.%s", menu->name, cmd);
> + if (cmd) {
> + int err;
> +
> + err = asprintf(&cmd, "%s.%s", menu->name, cmd);
> + if (err < 0)
> + return NULL;
> + }
>
> return cmd;
> }
> --
> 2.14.1
Applied, thanks.
--
Luiz Augusto von Dentz
Since asprintf() allocates new memory when a submenu command is
complemented, the memory leak occurs as below:
8 bytes in 1 blocks are definitely lost in loss record 18 of 179
at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x5679C99: strdup (strdup.c:42)
by 0x13B1E7: find_cmd.constprop.2 (shell.c:597)
by 0x13B2D1: cmd_generator (shell.c:646)
by 0x53B976D: rl_completion_matches (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
by 0x13BA91: shell_completion (shell.c:777)
by 0x53B98B6: ??? (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
by 0x53B9A99: rl_complete_internal (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
by 0x53B02EE: _rl_dispatch_subseq (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
by 0x53B07B5: readline_internal_char (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
by 0x53C8F84: rl_callback_read_char (in /lib/x86_64-linux-gnu/libreadline.so.7.0)
by 0x13AD80: input_read (shell.c:1065)
---
src/shared/shell.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/src/shared/shell.c b/src/shared/shell.c
index 7417e7ab4..9cd8d2567 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -645,10 +645,16 @@ static char *cmd_generator(const char *text, int state)
cmd = find_cmd(text + strlen(menu->name) + 1, menu->entries, &index);
if (cmd) {
int err;
+ char *tmp;
+
+ err = asprintf(&tmp, "%s.%s", menu->name, cmd);
+
+ free(cmd);
- err = asprintf(&cmd, "%s.%s", menu->name, cmd);
if (err < 0)
return NULL;
+
+ cmd = tmp;
}
return cmd;
--
2.14.1