From 6d2d18dfebaea74a30f263e743bdbd1fb14f0580 Mon Sep 17 00:00:00 2001 From: Simon Wilkinson Date: Fri, 15 Feb 2013 11:55:37 +0000 Subject: [PATCH] Fix incorrect sizeof() arguments in allocations In a number of places we have struct X *val; val = malloc(sizeof(struct Y)); If sizeof(struct Y) < sizeof(struct X) this is obviously dangerous, but it is incorrect regardless of the relative sizes of the structures. Fix all of the occurences of this that clang points out to us. Caught by clang-analyzer Change-Id: Iad32b4ae460d3f40a45cf33624973bf52fd167d4 Reviewed-on: http://gerrit.openafs.org/9156 Tested-by: BuildBot Reviewed-by: Derrick Brashear Reviewed-by: Chas Williams - CONTRACTOR Reviewed-by: Jeffrey Altman --- src/budb/procs.c | 4 ++-- src/libadmin/vos/vosutils.c | 2 +- src/libafscp/afscp_server.c | 3 ++- src/lwp/iomgr.c | 10 +++++----- src/rx/rx_multi.c | 2 +- src/viced/afsfileprocs.c | 2 +- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/budb/procs.c b/src/budb/procs.c index 868aea4bf..0338f4566 100644 --- a/src/budb/procs.c +++ b/src/budb/procs.c @@ -346,7 +346,7 @@ FreeReturnList(struct returnList *list) static afs_int32 AddToReturnList(struct returnList *list, dbadr a, afs_int32 *to_skipP) { - char *tmp; + dbadr *tmp; afs_int32 size; if (a == 0) @@ -372,7 +372,7 @@ AddToReturnList(struct returnList *list, dbadr a, afs_int32 *to_skipP) } if (!tmp) return BUDB_NOMEM; - list->elements = (dbadr *) tmp; + list->elements = tmp; list->allocSize = size; } diff --git a/src/libadmin/vos/vosutils.c b/src/libadmin/vos/vosutils.c index b00cc4ad8..b4ee7d44a 100644 --- a/src/libadmin/vos/vosutils.c +++ b/src/libadmin/vos/vosutils.c @@ -279,7 +279,7 @@ VLDB_ListAttributes(afs_cell_handle_p cellHandle, if (*entriesp > 0) { blkentriesp->nbulkentries_val = - calloc(*entriesp, sizeof(*blkentriesp)); + calloc(*entriesp, sizeof(struct nvldbentry)); if (blkentriesp->nbulkentries_val != NULL) { for (i = 0; i < *entriesp; i++) { OldVLDB_to_NewVLDB((struct vldbentry *)&arrayEntries. diff --git a/src/libafscp/afscp_server.c b/src/libafscp/afscp_server.c index 57e110789..c8b3fd7d8 100644 --- a/src/libafscp/afscp_server.c +++ b/src/libafscp/afscp_server.c @@ -369,7 +369,8 @@ afscp_ServerByAddr(struct afscp_cell *thecell, afs_uint32 addr) else thecell->srvsalloced = 4; newlist = realloc(thecell->fsservers, - thecell->srvsalloced * sizeof(struct afscp_server)); + thecell->srvsalloced + * sizeof(struct afscp_server *)); if (newlist == NULL) { return NULL; } diff --git a/src/lwp/iomgr.c b/src/lwp/iomgr.c index b7dd1c2b5..dcd94d158 100644 --- a/src/lwp/iomgr.c +++ b/src/lwp/iomgr.c @@ -152,19 +152,19 @@ void IOMGR_FreeFDSet(fd_set *s) */ fd_set *IOMGR_AllocFDSet(void) { - struct IOMGR_fd_set *t; + fd_set *t; if (iomgrFreeFDSets) { - t = iomgrFreeFDSets; + t = (fd_set *) iomgrFreeFDSets; iomgrFreeFDSets = iomgrFreeFDSets->next; } else { t = malloc(sizeof(fd_set)); } if (!t) - return (fd_set*)0; + return NULL; else { - FD_ZERO((fd_set*)t); - return (fd_set*)t; + FD_ZERO(t); + return t; } } diff --git a/src/rx/rx_multi.c b/src/rx/rx_multi.c index 65a347ed4..3362755c8 100644 --- a/src/rx/rx_multi.c +++ b/src/rx/rx_multi.c @@ -39,7 +39,7 @@ multi_Init(struct rx_connection **conns, int nConns) */ calls = osi_Alloc(sizeof(struct rx_call *) * nConns); - ready = osi_Alloc(sizeof(short *) * nConns); + ready = osi_Alloc(sizeof(short) * nConns); mh = osi_Alloc(sizeof(struct multi_handle)); if (!calls || !ready || !mh) osi_Panic("multi_Rx: no mem\n"); diff --git a/src/viced/afsfileprocs.c b/src/viced/afsfileprocs.c index b400c99cc..d1376f72c 100644 --- a/src/viced/afsfileprocs.c +++ b/src/viced/afsfileprocs.c @@ -5098,7 +5098,7 @@ SRXAFS_GetStatistics64(struct rx_call *acall, afs_int32 statsVersion, ViceStatis ViceLog(1, ("SAFS_GetStatistics64 Received\n")); Statistics->ViceStatistics64_val = - malloc(statsVersion*sizeof(afs_int64)); + malloc(statsVersion*sizeof(afs_uint64)); Statistics->ViceStatistics64_len = statsVersion; FS_LOCK; AFSCallStats.GetStatistics++, AFSCallStats.TotalCalls++; -- 2.39.5