tests/cmdline: fix memory leaks
Checks
Commit Message
From: Owen Hilyard <ohilyard@iol.unh.edu>
Fixes for a few memory leaks in the cmdline_autotest unit test.
All of the leaks were related to not freeing the commandline struct
after testing had completed.
Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
---
app/test/test_cmdline_lib.c | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
Comments
On Wed, Jun 16, 2021 at 6:26 PM <ohilyard@iol.unh.edu> wrote:
>
> From: Owen Hilyard <ohilyard@iol.unh.edu>
>
> Fixes for a few memory leaks in the cmdline_autotest unit test.
>
> All of the leaks were related to not freeing the commandline struct
> after testing had completed.
We will need a Fixes: tag and Cc: stable.
>
> Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
> ---
> app/test/test_cmdline_lib.c | 32 ++++++++++++++++++++++++--------
> 1 file changed, 24 insertions(+), 8 deletions(-)
>
> diff --git a/app/test/test_cmdline_lib.c b/app/test/test_cmdline_lib.c
> index bd72df0da..fd0a797c1 100644
> --- a/app/test/test_cmdline_lib.c
> +++ b/app/test/test_cmdline_lib.c
> @@ -71,10 +71,12 @@ test_cmdline_parse_fns(void)
> if (cmdline_complete(cl, "buffer", &i, NULL, sizeof(dst)) >= 0)
> goto error;
>
> + cmdline_free(cl);
> return 0;
>
> error:
> printf("Error: function accepted null parameter!\n");
> + cmdline_free(cl);
> return -1;
> }
>
> @@ -140,32 +142,45 @@ static int
> test_cmdline_socket_fns(void)
> {
> cmdline_parse_ctx_t ctx;
> + struct cmdline *cl;
>
> - if (cmdline_stdin_new(NULL, "prompt") != NULL)
> + cl = cmdline_stdin_new(NULL, "prompt");
> + if (cl != NULL)
> goto error;
> - if (cmdline_stdin_new(&ctx, NULL) != NULL)
> + cl = cmdline_stdin_new(&ctx, NULL);
> + if (cl != NULL)
> goto error;
> - if (cmdline_file_new(NULL, "prompt", "/dev/null") != NULL)
> + cl = cmdline_file_new(NULL, "prompt", "/dev/null");
> + if (cl != NULL)
> goto error;
> - if (cmdline_file_new(&ctx, NULL, "/dev/null") != NULL)
> + cl = cmdline_file_new(&ctx, NULL, "/dev/null");
> + if (cl != NULL)
> goto error;
> - if (cmdline_file_new(&ctx, "prompt", NULL) != NULL)
> + cl = cmdline_file_new(&ctx, "prompt", NULL);
> + if (cl != NULL)
> goto error;
> - if (cmdline_file_new(&ctx, "prompt", "-/invalid/~/path") != NULL) {
> + cl = cmdline_file_new(&ctx, "prompt", "-/invalid/~/path");
> + if (cl != NULL) {
> printf("Error: succeeded in opening invalid file for reading!");
> + cmdline_free(cl);
> return -1;
> }
> - if (cmdline_file_new(&ctx, "prompt", "/dev/null") == NULL) {
> + cl = cmdline_file_new(&ctx, "prompt", "/dev/null");
> + if (cl == NULL) {
> printf("Error: failed to open /dev/null for reading!");
> + cmdline_free(cl);
> return -1;
> }
>
> /* void functions */
> cmdline_stdin_exit(NULL);
> -
> + if (cl != NULL)
> + cmdline_free(cl);
We made sure cl != NULL above, no need for this test.
> return 0;
> error:
> printf("Error: function accepted null parameter!\n");
> + if (cl != NULL)
> + cmdline_free(cl);
And anyway, cmdline_free() handles a NULL pointer fine, so you can
also remove the check here.
With commitlog updated and those two changes, you can add my:
Reviewed-by: David Marchand <david.marchand@redhat.com>
> return -1;
> }
>
> @@ -198,6 +213,7 @@ test_cmdline_fns(void)
> cmdline_interact(NULL);
> cmdline_quit(NULL);
>
> + cmdline_free(cl);
> return 0;
>
> error:
> --
> 2.30.2
>
@@ -71,10 +71,12 @@ test_cmdline_parse_fns(void)
if (cmdline_complete(cl, "buffer", &i, NULL, sizeof(dst)) >= 0)
goto error;
+ cmdline_free(cl);
return 0;
error:
printf("Error: function accepted null parameter!\n");
+ cmdline_free(cl);
return -1;
}
@@ -140,32 +142,45 @@ static int
test_cmdline_socket_fns(void)
{
cmdline_parse_ctx_t ctx;
+ struct cmdline *cl;
- if (cmdline_stdin_new(NULL, "prompt") != NULL)
+ cl = cmdline_stdin_new(NULL, "prompt");
+ if (cl != NULL)
goto error;
- if (cmdline_stdin_new(&ctx, NULL) != NULL)
+ cl = cmdline_stdin_new(&ctx, NULL);
+ if (cl != NULL)
goto error;
- if (cmdline_file_new(NULL, "prompt", "/dev/null") != NULL)
+ cl = cmdline_file_new(NULL, "prompt", "/dev/null");
+ if (cl != NULL)
goto error;
- if (cmdline_file_new(&ctx, NULL, "/dev/null") != NULL)
+ cl = cmdline_file_new(&ctx, NULL, "/dev/null");
+ if (cl != NULL)
goto error;
- if (cmdline_file_new(&ctx, "prompt", NULL) != NULL)
+ cl = cmdline_file_new(&ctx, "prompt", NULL);
+ if (cl != NULL)
goto error;
- if (cmdline_file_new(&ctx, "prompt", "-/invalid/~/path") != NULL) {
+ cl = cmdline_file_new(&ctx, "prompt", "-/invalid/~/path");
+ if (cl != NULL) {
printf("Error: succeeded in opening invalid file for reading!");
+ cmdline_free(cl);
return -1;
}
- if (cmdline_file_new(&ctx, "prompt", "/dev/null") == NULL) {
+ cl = cmdline_file_new(&ctx, "prompt", "/dev/null");
+ if (cl == NULL) {
printf("Error: failed to open /dev/null for reading!");
+ cmdline_free(cl);
return -1;
}
/* void functions */
cmdline_stdin_exit(NULL);
-
+ if (cl != NULL)
+ cmdline_free(cl);
return 0;
error:
printf("Error: function accepted null parameter!\n");
+ if (cl != NULL)
+ cmdline_free(cl);
return -1;
}
@@ -198,6 +213,7 @@ test_cmdline_fns(void)
cmdline_interact(NULL);
cmdline_quit(NULL);
+ cmdline_free(cl);
return 0;
error: