diff mbox series

[OpenWrt-Devel,rpcd,v2,6/6] main: exec_self: make clang analyzer happy

Message ID 20191021125924.37223-2-yszhou4tech@gmail.com
State Accepted
Headers show
Series memory issue fixes | expand

Commit Message

Yousong Zhou Oct. 21, 2019, 12:59 p.m. UTC
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
---
 main.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Comments

Petr Štetiar Oct. 22, 2019, 4:26 a.m. UTC | #1
Yousong Zhou <yszhou4tech@gmail.com> [2019-10-21 12:59:24]:

almost happy:

 main.c:65:1: warning: Potential leak of memory pointed to by 'args'

diff --git a/main.c b/main.c
index 12cb4c5c720c..8b11418f1c09 100644
--- a/main.c
+++ b/main.c
@@ -62,6 +62,7 @@ exec_self(int argc, char **argv)
 
 	setenv("RPC_HANGUP", "1", 1);
 	execv(cmd, (char * const *)args);
+	free(args);
 }

> Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
> ---
>  main.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/main.c b/main.c
> index 1db3241..12cb4c5 100644
> --- a/main.c
> +++ b/main.c
> @@ -47,12 +47,16 @@ static void
>  exec_self(int argc, char **argv)
>  {
>  	int i;
> -	const char *cmd = rpc_exec_lookup(argv[0]);
> -	char **args = calloc(argc + 1, sizeof(char *));
> +	const char *cmd;
> +	char **args;
>  
> -	if (!cmd || !args)
> +	cmd = rpc_exec_lookup(argv[0]);
> +	if (!cmd)
>  		return;
>  
> +	args = calloc(argc + 1, sizeof(char *));
> +	if (!args)
> +		return;
>  	for (i = 0; i < argc; i++)
>  		args[i] = argv[i];
>  
>
Petr Štetiar Oct. 22, 2019, 4:49 a.m. UTC | #2
Petr Štetiar <ynezz@true.cz> [2019-10-22 06:26:14]:

> Yousong Zhou <yszhou4tech@gmail.com> [2019-10-21 12:59:24]:
> 
> almost happy:
> 
>  main.c:65:1: warning: Potential leak of memory pointed to by 'args'
> 
> diff --git a/main.c b/main.c
> index 12cb4c5c720c..8b11418f1c09 100644
> --- a/main.c
> +++ b/main.c
> @@ -62,6 +62,7 @@ exec_self(int argc, char **argv)
>  
>  	setenv("RPC_HANGUP", "1", 1);
>  	execv(cmd, (char * const *)args);
> +	free(args);
>  }

So I went ahead and I've put some more fixes on top of yours[1] and it's all
green now[2]. I've only compiled test it, so the question is, if it's still
going to work :-)

1. https://gitlab.com/ynezz/openwrt-rpcd/commits/yszhou4tech/memory-issues-fixes
2. https://gitlab.com/ynezz/openwrt-rpcd/pipelines/90432330

-- ynezz
Yousong Zhou Oct. 22, 2019, 5:24 a.m. UTC | #3
On Tue, 22 Oct 2019 at 12:49, Petr Štetiar <ynezz@true.cz> wrote:
>
> Petr Štetiar <ynezz@true.cz> [2019-10-22 06:26:14]:
>
> > Yousong Zhou <yszhou4tech@gmail.com> [2019-10-21 12:59:24]:
> >
> > almost happy:
> >
> >  main.c:65:1: warning: Potential leak of memory pointed to by 'args'
> >
> > diff --git a/main.c b/main.c
> > index 12cb4c5c720c..8b11418f1c09 100644
> > --- a/main.c
> > +++ b/main.c
> > @@ -62,6 +62,7 @@ exec_self(int argc, char **argv)
> >
> >       setenv("RPC_HANGUP", "1", 1);
> >       execv(cmd, (char * const *)args);
> > +     free(args);
> >  }

Good catch ;)

>
> So I went ahead and I've put some more fixes on top of yours[1] and it's all
> green now[2]. I've only compiled test it, so the question is, if it's still
> going to work :-)
>
> 1. https://gitlab.com/ynezz/openwrt-rpcd/commits/yszhou4tech/memory-issues-fixes
> 2. https://gitlab.com/ynezz/openwrt-rpcd/pipelines/90432330
>
> -- ynezz

Thanks for the review.  I added a few comments there.

Since you have got the tools ready, please feel free to take over the
series and make fixup, squashes as you like ;)  Meanwhile, please
consider sending updated patches to the mailing list for easier
review.

Regards,
                yousong
diff mbox series

Patch

diff --git a/main.c b/main.c
index 1db3241..12cb4c5 100644
--- a/main.c
+++ b/main.c
@@ -47,12 +47,16 @@  static void
 exec_self(int argc, char **argv)
 {
 	int i;
-	const char *cmd = rpc_exec_lookup(argv[0]);
-	char **args = calloc(argc + 1, sizeof(char *));
+	const char *cmd;
+	char **args;
 
-	if (!cmd || !args)
+	cmd = rpc_exec_lookup(argv[0]);
+	if (!cmd)
 		return;
 
+	args = calloc(argc + 1, sizeof(char *));
+	if (!args)
+		return;
 	for (i = 0; i < argc; i++)
 		args[i] = argv[i];