]> git.michaelhowe.org Git - packages/o/openafs.git/commitdiff
Don't allocate objects of 0 length
authorSimon Wilkinson <sxw@your-file-system.com>
Fri, 15 Feb 2013 11:37:47 +0000 (11:37 +0000)
committerJeffrey Altman <jaltman@your-file-system.com>
Thu, 21 Feb 2013 16:50:47 +0000 (08:50 -0800)
Fix assorted places in the code where we might have asked malloc to
allocate a block of zero length.

Caught by clang-analyzer

Change-Id: I6e1226ad83a52984ee7c53cbed8c867f38e4f866
Reviewed-on: http://gerrit.openafs.org/9155
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/budb/procs.c
src/libadmin/vos/vosutils.c
src/tools/dumpscan/dumptool.c
src/viced/callback.c
src/xstat/xstat_cm_test.c
src/xstat/xstat_fs_test.c

index 90afeaebe4388cd2c0b4ce7369d808f11032117b..868aea4bfb024c210c027ca85e04cd69962ee010 100644 (file)
@@ -534,9 +534,12 @@ SendReturnList(struct ubik_trans *ut,
 
     /* Allocate space for the return values if needed and zero it */
     if (eList->budb_dumpList_val == 0) {
-       eList->budb_dumpList_val = calloc(to_return, e_size);
-       if (!eList->budb_dumpList_val)
-           return (BUDB_NOMEM);
+       if (to_return > 0) {
+           eList->budb_dumpList_val = calloc(to_return, e_size);
+           if (!eList->budb_dumpList_val)
+               return (BUDB_NOMEM);
+       } else
+           eList->budb_dumpList_val = NULL;
     } else {
         memset(eList->budb_dumpList_val, 0, e_size * to_return);
     }
index 3fcab998909e3578cd988a0e13aff8ef9bfc0f55..b00cc4ad84d1dbc933b855cb8298d02adf6546d0 100644 (file)
@@ -277,22 +277,25 @@ VLDB_ListAttributes(afs_cell_handle_p cellHandle,
                if (*entriesp > arrayEntries.bulkentries_len)
                    *entriesp = arrayEntries.bulkentries_len;
 
-               blkentriesp->nbulkentries_val =
-                   malloc(*entriesp * sizeof(*blkentriesp));
-               if (blkentriesp->nbulkentries_val != NULL) {
-                   for (i = 0; i < *entriesp; i++) {
-                       OldVLDB_to_NewVLDB((struct vldbentry *)&arrayEntries.
-                                          bulkentries_val[i],
-                                          (struct nvldbentry *)&blkentriesp->
-                                          nbulkentries_val[i], &tst);
+               if (*entriesp > 0) {
+                   blkentriesp->nbulkentries_val =
+                       calloc(*entriesp, sizeof(*blkentriesp));
+                   if (blkentriesp->nbulkentries_val != NULL) {
+                       for (i = 0; i < *entriesp; i++) {
+                           OldVLDB_to_NewVLDB((struct vldbentry *)&arrayEntries.
+                                              bulkentries_val[i],
+                                              (struct nvldbentry *)&blkentriesp->
+                                              nbulkentries_val[i], &tst);
+                       }
+                   } else {
+                       tst = ADMNOMEM;
                    }
                } else {
-                   tst = ADMNOMEM;
-               }
-               if (arrayEntries.bulkentries_val) {
-                   free(arrayEntries.bulkentries_val);
+                   blkentriesp->nbulkentries_val = NULL;
                }
 
+               xdr_free((xdrproc_t)xdr_bulkentries, &arrayEntries);
+
                rc = 1;
            }
        }
index 6f6e44fef391dbb76cb951fe2fda511bb36b1c44..a8c05a05155d4086cdc8f91d70d8c18c20cf4d33 100644 (file)
@@ -1149,12 +1149,19 @@ ScanVnodes(FILE * f, VolumeDiskData * vol, int sizescan)
        numSmallVnodes = numFileVnodes;
 
     } else {
-       LargeVnodeIndex = (struct vnodeData **)
-           malloc(numDirVnodes * sizeof(struct vnodeData));
-       SmallVnodeIndex = (struct vnodeData **)
-           malloc(numFileVnodes * sizeof(struct vnodeData));
+       if (numDirVnodes == 0)
+           LargeVnodeIndex = NULL;
+       else
+           LargeVnodeIndex = malloc(numDirVnodes
+                                     * sizeof(struct vnodeData *));
+       if (numFileVnodes == 0)
+           SmallVnodeIndex = NULL;
+       else
+           SmallVnodeIndex = malloc(numFileVnodes
+                                     * sizeof(struct vnodeData *));
 
-       if (LargeVnodeIndex == NULL || SmallVnodeIndex == NULL) {
+       if ((numDirVnodes != 0 && LargeVnodeIndex == NULL) ||
+           (numFileVnodes != 0 && SmallVnodeIndex == NULL)) {
            if (verbose)
                fprintf(stderr,
                        "Unable to allocate space " "for vnode tables\n");
index 3c21a494a7ea975dde6fec655f4228b434b95d4c..5460702dbd91bfadc2a8c410db66ef1d036a6c53 100644 (file)
@@ -422,11 +422,13 @@ FDel(struct FileEntry *fe)
 int
 InitCallBack(int nblks)
 {
+    opr_Assert(nblks > 0);
+
     H_LOCK;
     tfirst = CBtime(time(NULL));
     /* N.B. The "-1", below, is because
      * FE[0] and CB[0] are not used--and not allocated */
-    FE = ((struct FileEntry *)(calloc(nblks, sizeof(struct FileEntry))));
+    FE = calloc(nblks, sizeof(struct FileEntry));
     if (!FE) {
        ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
     }
@@ -434,7 +436,7 @@ InitCallBack(int nblks)
     cbstuff.nFEs = nblks;
     while (cbstuff.nFEs)
        FreeFE(&FE[cbstuff.nFEs]);      /* This is correct */
-    CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack))));
+    CB = calloc(nblks, sizeof(struct CallBack));
     if (!CB) {
        ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
     }
index ce76972e39d76cd023825da6743723a76247d470..bb1d5ad70e16264a964858f537050d0c88d24bd8 100644 (file)
@@ -790,11 +790,15 @@ RunTheTest(struct cmd_syndesc *a_s, void *arock)
     if (debugging_on)
        printf("%s: Allocating socket array for %d Cache Manager(s)\n", rn,
               numCMs);
-    CMSktArray = malloc(numCMs * sizeof(struct sockaddr_in));
-    if (CMSktArray == (struct sockaddr_in *)0) {
-       printf("%s: Can't allocate socket array for %d Cache Managers\n", rn,
-              numCMs);
-       exit(1);
+    if (numCMs > 0) {
+       CMSktArray = calloc(numCMs, sizeof(struct sockaddr_in));
+       if (CMSktArray == NULL) {
+           printf("%s: Can't allocate socket array for %d Cache Managers\n",
+                  rn, numCMs);
+           exit(1);
+       }
+    } else {
+       CMSktArray = NULL;
     }
 
     /*
@@ -824,7 +828,12 @@ RunTheTest(struct cmd_syndesc *a_s, void *arock)
      */
     if (debugging_on)
        printf("Allocating %d long(s) for coll ID\n", numCollIDs);
-    collIDP = malloc(numCollIDs * sizeof(afs_int32));
+
+    if (numCollIDs > 0)
+       collIDP = calloc(numCollIDs, sizeof(afs_int32));
+    else
+       collIDP = NULL;
+
     currCollIDP = collIDP;
     curr_item = a_s->parms[P_COLL_IDS].items;
     for (currCollIDIdx = 0; currCollIDIdx < numCollIDs; currCollIDIdx++) {
index 413d15cbf7bf6c683d8caa1929a3a2c81e2ae8cb..d1e1fa3d0a9dd2c115b2fd66184481d414ed01b0 100644 (file)
@@ -726,7 +726,12 @@ RunTheTest(struct cmd_syndesc *a_s, void *dummy)
      */
     if (debugging_on)
        printf("Allocating %d long(s) for coll ID\n", numCollIDs);
-    collIDP = malloc(numCollIDs * sizeof(afs_int32));
+
+    if (numCollIDs > 0)
+       collIDP = calloc(numCollIDs, sizeof(afs_int32));
+    else
+       collIDP = NULL;
+
     currCollIDP = collIDP;
     curr_item = a_s->parms[P_COLL_IDS].items;
     for (currCollIDIdx = 0; currCollIDIdx < numCollIDs; currCollIDIdx++) {