From: Simon Wilkinson Date: Tue, 19 Feb 2013 15:46:52 +0000 (+0000) Subject: ptserver: Tidy malloc handling in readpwd X-Git-Tag: upstream/1.8.0_pre1^2~1500 X-Git-Url: https://git.michaelhowe.org/gitweb/?a=commitdiff_plain;h=df0b2d6775523d12fe18043bf392c80533c7b38a;p=packages%2Fo%2Fopenafs.git ptserver: Tidy malloc handling in readpwd Tidy up the malloc handling in readpwd, so that we don't leak memory if the user specifies multiple -c arguments. Also avoid assuming that free(NULL) will always work. Change-Id: I95f3fe908572cb5be2d30345ccae0a2858622bd5 Reviewed-on: http://gerrit.openafs.org/9178 Reviewed-by: Derrick Brashear Tested-by: BuildBot Reviewed-by: Jeffrey Altman --- diff --git a/src/ptserver/readpwd.c b/src/ptserver/readpwd.c index 0713d5d28..e0c5c28f9 100644 --- a/src/ptserver/readpwd.c +++ b/src/ptserver/readpwd.c @@ -53,26 +53,27 @@ main(afs_int32 argc, char **argv) char uid[8]; afs_int32 i; afs_int32 verbose = 0; - char *cellname; + char *cellname = NULL; if (argc < 2) { fprintf(stderr, "Usage: readpwd [-v] [-c cellname] passwdfile.\n"); exit(1); } - cellname = 0; for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-v")) verbose = 1; else { if (!strcmp(argv[i], "-c")) { - cellname = malloc(100); + if (!cellname) + cellname = malloc(100); strncpy(cellname, argv[++i], 100); } else strncpy(buf, argv[i], 150); } } code = pr_Initialize(2, AFSDIR_CLIENT_ETC_DIRPATH, cellname); - free(cellname); + if (cellname) + free(cellname); if (code) { fprintf(stderr, "pr_Initialize failed, code %d.\n", code); exit(1);