Re: [PATCH] rpcdebug: Add proper free() to avoid memory leak

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 07/27/2017 03:59 AM, ChunYu Wang wrote:
> rpcdebug.c use char* cdename to store program name:
> 
>     cdename = malloc(strlen(basename(argv[0])));
>     strcpy(cdename, basename(argv[0]));
> 
> It is better to free before exit to avoid potential leaks.
I guess I don't understand how memory can be leaked
when a process is exiting... What am I missing?

steved.
> 
> Signed-off-by: ChunYu Wang <chunyu.wang.1995@xxxxxxxxx>
> ---
>  tools/rpcdebug/rpcdebug.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/tools/rpcdebug/rpcdebug.c b/tools/rpcdebug/rpcdebug.c
> index 68206cc..722ab4d 100644
> --- a/tools/rpcdebug/rpcdebug.c
> +++ b/tools/rpcdebug/rpcdebug.c
> @@ -131,6 +131,7 @@ main(int argc, char **argv)
>  	        print_flags(stdout, module, ~(unsigned int) 0, 1);
>  	}
>  
> +	free(cdename);
>  	return 0;
>  }
>  
> @@ -221,6 +222,7 @@ find_flag(char **module, char *name)
>  				"please specify the module name using\n"
>  				"the -m option.\n",
>  				cdename, name);
> +			free(cdename);
>  			exit(1);
>  		}
>  		value = flagmap[i].value;
> @@ -238,6 +240,7 @@ find_flag(char **module, char *name)
>  			fprintf(stderr,
>  				"%s: unknown flag %s\n",
>  				cdename, name);
> +		free(cdename);
>  		exit(1);
>  	}
>  
> @@ -255,10 +258,12 @@ get_flags(char *module)
>  
>  	if ((sysfd = open(filename, O_RDONLY)) < 0) {
>  		perror(filename);
> +		free(cdename);
>  		exit(1);
>  	}
>  	if ((len = read(sysfd, buffer, sizeof(buffer))) < 0) {
>  		perror("read");
> +		free(cdename);
>  		exit(1);
>  	}
>  	close(sysfd);
> @@ -278,14 +283,17 @@ set_flags(char *module, unsigned int value)
>  	len = sprintf(buffer, "%d", value);
>  	if ((sysfd = open(filename, O_WRONLY)) < 0) {
>  		perror(filename);
> +		free(cdename);
>  		exit(1);
>  	}
>  	if ((ret = write(sysfd, buffer, len)) < 0) {
>  		perror("write");
> +		free(cdename);
>  		exit(1);
>  	}
>  	if (ret < len) {
>  		fprintf(stderr, "error: short write in set_flags!\n");
> +		free(cdename);
>  		exit(1);
>  	}
>  	close(sysfd);
> @@ -359,6 +367,7 @@ usage(int excode, char *module)
>  	  else
>  	    fprintf(stderr, "       (use %s -vh to get a list of modules and valid flags)\n", cdename);
>  	}
> +	free(cdename);
>  	exit (excode);
>  }
>  
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Filesystem Development]     [Linux USB Development]     [Linux Media Development]     [Video for Linux]     [Linux NILFS]     [Linux Audio Users]     [Yosemite Info]     [Linux SCSI]

  Powered by Linux