ptserver: Tidy malloc handling in readpwd
authorSimon Wilkinson <sxw@your-file-system.com>
Tue, 19 Feb 2013 15:46:52 +0000 (15:46 +0000)
committerJeffrey Altman <jaltman@your-file-system.com>
Fri, 22 Feb 2013 06:20:05 +0000 (22:20 -0800)
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 <shadow@your-file-system.com>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Jeffrey Altman <jaltman@your-file-system.com>

src/ptserver/readpwd.c

index 0713d5d..e0c5c28 100644 (file)
@@ -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);