[v3] tests/cmdline: fix memory leaks

Message ID 20210623180645.91942-1-ohilyard@iol.unh.edu (mailing list archive)
State Accepted, archived
Delegated to: David Marchand
Headers
Series [v3] tests/cmdline: fix memory leaks |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/github-robot success github build: passed
ci/iol-abi-testing success Testing PASS
ci/iol-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS

Commit Message

Owen Hilyard June 23, 2021, 6:06 p.m. UTC
  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.

Fixes: dbb860e03e ("cmdline: tests")

Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
Reviewed-by: David Marchand <david.marchand@redhat.com>
---
 app/test/test_cmdline_lib.c | 40 ++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 12 deletions(-)
  

Comments

Olivier Matz June 24, 2021, 12:07 p.m. UTC | #1
Hi Owen,

One small issue remain, please see below.

On Wed, Jun 23, 2021 at 02:06:45PM -0400, 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.
> 
> Fixes: dbb860e03e ("cmdline: tests")
> 
> Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
> Reviewed-by: David Marchand <david.marchand@redhat.com>
> ---
>  app/test/test_cmdline_lib.c | 40 ++++++++++++++++++++++++++-----------
>  1 file changed, 28 insertions(+), 12 deletions(-)
> 
> diff --git a/app/test/test_cmdline_lib.c b/app/test/test_cmdline_lib.c
> index bd72df0da..b476b2594 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,44 @@ 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;
>  	}

The cmdline_free(cl) after a if (cl == NULL) is not needed.

After that change, you can add my ack in v4.

Thanks,
Olivier

>  
>  	/* void functions */
>  	cmdline_stdin_exit(NULL);
>  
> +	cmdline_free(cl);
>  	return 0;
>  error:
>  	printf("Error: function accepted null parameter!\n");
> +	cmdline_free(cl);
>  	return -1;
>  }
>  
> @@ -176,13 +190,14 @@ test_cmdline_fns(void)
>  	struct cmdline *cl;
>  
>  	memset(&ctx, 0, sizeof(ctx));
> -	cl = cmdline_new(&ctx, "test", -1, -1);
> -	if (cl == NULL)
> +	cl = cmdline_new(NULL, "prompt", 0, 0);
> +	if (cl != NULL)
>  		goto error;
> -
> -	if (cmdline_new(NULL, "prompt", 0, 0) != NULL)
> +	cl = cmdline_new(&ctx, NULL, 0, 0);
> +	if (cl != NULL)
>  		goto error;
> -	if (cmdline_new(&ctx, NULL, 0, 0) != NULL)
> +	cl = cmdline_new(&ctx, "test", -1, -1);
> +	if (cl == NULL)
>  		goto error;
>  	if (cmdline_in(NULL, "buffer", CMDLINE_TEST_BUFSIZE) >= 0)
>  		goto error;
> @@ -198,6 +213,7 @@ test_cmdline_fns(void)
>  	cmdline_interact(NULL);
>  	cmdline_quit(NULL);
>  
> +	cmdline_free(cl);
>  	return 0;
>  
>  error:
> -- 
> 2.30.2
>
  
David Marchand June 24, 2021, 1:24 p.m. UTC | #2
On Thu, Jun 24, 2021 at 2:08 PM Olivier Matz <olivier.matz@6wind.com> wrote:
> > -     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;
> >       }
>
> The cmdline_free(cl) after a if (cl == NULL) is not needed.

I took the liberty of doing the change when applying and added your ack.
Thanks.
  
David Marchand June 24, 2021, 1:25 p.m. UTC | #3
On Wed, Jun 23, 2021 at 8:06 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.
>
> Fixes: dbb860e03e ("cmdline: tests")
Updated sha1.
Cc: stable@dpdk.org

>
> Signed-off-by: Owen Hilyard <ohilyard@iol.unh.edu>
> Reviewed-by: David Marchand <david.marchand@redhat.com>
Acked-by: Olivier Matz <olivier.matz@6wind.com>

Applied, thanks.
  

Patch

diff --git a/app/test/test_cmdline_lib.c b/app/test/test_cmdline_lib.c
index bd72df0da..b476b2594 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,44 @@  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);
 
+	cmdline_free(cl);
 	return 0;
 error:
 	printf("Error: function accepted null parameter!\n");
+	cmdline_free(cl);
 	return -1;
 }
 
@@ -176,13 +190,14 @@  test_cmdline_fns(void)
 	struct cmdline *cl;
 
 	memset(&ctx, 0, sizeof(ctx));
-	cl = cmdline_new(&ctx, "test", -1, -1);
-	if (cl == NULL)
+	cl = cmdline_new(NULL, "prompt", 0, 0);
+	if (cl != NULL)
 		goto error;
-
-	if (cmdline_new(NULL, "prompt", 0, 0) != NULL)
+	cl = cmdline_new(&ctx, NULL, 0, 0);
+	if (cl != NULL)
 		goto error;
-	if (cmdline_new(&ctx, NULL, 0, 0) != NULL)
+	cl = cmdline_new(&ctx, "test", -1, -1);
+	if (cl == NULL)
 		goto error;
 	if (cmdline_in(NULL, "buffer", CMDLINE_TEST_BUFSIZE) >= 0)
 		goto error;
@@ -198,6 +213,7 @@  test_cmdline_fns(void)
 	cmdline_interact(NULL);
 	cmdline_quit(NULL);
 
+	cmdline_free(cl);
 	return 0;
 
 error: