[dpdk-stable] [PATCH 19.11] tests/cmdline: fix memory leaks

Christian Ehrhardt christian.ehrhardt at canonical.com
Wed Aug 11 08:43:27 CEST 2021


On Tue, Aug 10, 2021 at 7:40 PM <ohilyard at iol.unh.edu> wrote:
>
> From: Owen Hilyard <ohilyard at iol.unh.edu>
>
> [ upstream commit ca7204b921c2f328ab1222772af40922970e7c4b ]
>
> Fixes for a few memory leaks in the cmdline_autotest unit test.

Thank you for the backport - applied to the preliminary 19.11 branch
for 19.11.10

> All of the leaks were related to not freeing the commandline struct
> after testing had completed.
>
> Fixes: dbb860e03e ("cmdline: tests")
>
> Signed-off-by: Owen Hilyard <ohilyard at iol.unh.edu>
> Reviewed-by: David Marchand <david.marchand at redhat.com>
> ---
>  app/test/test_cmdline_lib.c | 43 ++++++++++++++++++++++++++-----------
>  1 file changed, 31 insertions(+), 12 deletions(-)
>
> diff --git a/app/test/test_cmdline_lib.c b/app/test/test_cmdline_lib.c
> index a856a9713..c7ada9ddd 100644
> --- a/app/test/test_cmdline_lib.c
> +++ b/app/test/test_cmdline_lib.c
> @@ -62,10 +62,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;
>  }
>
> @@ -131,32 +133,43 @@ 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") != NULL);
> +       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);
> -
> +       cmdline_free(cl);
>         return 0;
>  error:
>         printf("Error: function accepted null parameter!\n");
> +       cmdline_free(cl);
>         return -1;
>  }
>
> @@ -164,16 +177,18 @@ static int
>  test_cmdline_fns(void)
>  {
>         cmdline_parse_ctx_t ctx;
> -       struct cmdline cl, *tmp;
> +       struct cmdline cl, *tmp, *tmp2;
>
>         memset(&ctx, 0, sizeof(ctx));
>         tmp = cmdline_new(&ctx, "test", -1, -1);
>         if (tmp == NULL)
>                 goto error;
>
> -       if (cmdline_new(NULL, "prompt", 0, 0) != NULL)
> +       tmp2 = cmdline_new(NULL, "prompt", 0, 0);
> +       if (tmp2 != NULL)
>                 goto error;
> -       if (cmdline_new(&ctx, NULL, 0, 0) != NULL)
> +       tmp2 = cmdline_new(&ctx, NULL, 0, 0);
> +       if (tmp2 != NULL)
>                 goto error;
>         if (cmdline_in(NULL, "buffer", CMDLINE_TEST_BUFSIZE) >= 0)
>                 goto error;
> @@ -202,14 +217,18 @@ test_cmdline_fns(void)
>         if (memcmp(&cl, tmp, sizeof(cl))) goto mismatch;
>
>         cmdline_free(tmp);
> -
> +       cmdline_free(tmp2);
>         return 0;
>
>  error:
>         printf("Error: function accepted null parameter!\n");
> +       cmdline_free(tmp);
> +       cmdline_free(tmp2);
>         return -1;
>  mismatch:
>         printf("Error: data changed!\n");
> +       cmdline_free(tmp);
> +       cmdline_free(tmp2);
>         return -1;
>  }
>
> --
> 2.30.2
>


-- 
Christian Ehrhardt
Staff Engineer, Ubuntu Server
Canonical Ltd


More information about the stable mailing list