pts: Don't malloc(0) when there's nothing to do
authorSimon Wilkinson <sxw@your-file-system.com>
Sat, 31 Mar 2012 18:42:39 +0000 (14:42 -0400)
committerDerrick Brashear <shadow@dementix.org>
Mon, 9 Apr 2012 01:21:25 +0000 (18:21 -0700)
If GetNameOrId is called with no work to do, then don't attempt to
malloc a load of 0 length strings. Instead just return an empty array
to the caller.

Change-Id: I245cfde71d65b8a3b6df4217b90dad81e9e60a58
Reviewed-on: http://gerrit.openafs.org/7100
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@dementix.org>

src/ptserver/pts.c

index 276ff7b..b6601d6 100644 (file)
@@ -471,16 +471,26 @@ GetNameOrId(struct cmd_syndesc *as, struct idlist *lids,
     struct idlist ids, tids;   /* local copy, if not ret. ids */
     int goodCount = 0;
 
+    /* Initialise our outputs */
+    memset(lids, 0, sizeof(struct idlist));
+    if (lnames)
+       memset(lnames, 0, sizeof(struct namelist));
+
     for (i = as->parms[0].items; i; i = i->next)
        n++;
-    lids->idlist_val = (afs_int32 *) malloc(n * sizeof(afs_int32));
+
+    /* Nothing to do, so bail */
+    if (n == 0)
+       return 0;
+
+    lids->idlist_val = malloc(n * sizeof(afs_int32));
     lids->idlist_len = n;
-    ids.idlist_val = (afs_int32 *) malloc(n * sizeof(afs_int32));
+    ids.idlist_val = malloc(n * sizeof(afs_int32));
     ids.idlist_len = n;
-    names.namelist_val = (prname *) malloc(n * PR_MAXNAMELEN);
+    names.namelist_val = malloc(n * PR_MAXNAMELEN);
     names.namelist_len = n;
     if (lnames) {
-       lnames->namelist_val = (prname *) malloc(n * PR_MAXNAMELEN);
+       lnames->namelist_val = malloc(n * PR_MAXNAMELEN);
        lnames->namelist_len = 0;
     }
     for (i = as->parms[0].items; i; i = i->next) {