From e43c4fe66622769d9243e27cfd223511e376c503 Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Wed, 20 Nov 2002 23:13:09 +0000 Subject: [PATCH] Use calloc() instead of malloc() followed by memset to 0. Bug 124302, patch by Aaron Lehmann , r=timeless, sr=bzbarsky --- config/mantomak.c | 12 +- dbm/src/nsres.c | 307 ------------------ .../activex/src/pluginhostctrl/npn.cpp | 3 +- gfx/src/xlibrgb/xlibrgb.c | 6 +- mailnews/mime/src/mimefilt.cpp | 3 +- rdf/opendir/genopendir.c | 24 +- rdf/opendir/rdfparse.c | 4 +- rdf/opendir/spf2ldiff.c | 4 +- rdf/opendir/test.c | 3 +- widget/src/os2/nsFilePicker.cpp | 3 +- xpinstall/wizard/unix/src2/nsXIEngine.cpp | 3 +- xpinstall/wizard/windows/ds32/ds32.cpp | 3 +- 12 files changed, 19 insertions(+), 356 deletions(-) diff --git a/config/mantomak.c b/config/mantomak.c index 8ae22e0b87e..ac8acbd5883 100644 --- a/config/mantomak.c +++ b/config/mantomak.c @@ -707,15 +707,13 @@ void add_macro(char *pString, macro_list **ppList) /* Allocate a new list entry for the macro. */ - pEntry = (macro_list *)malloc(sizeof(macro_list)); - memset(pEntry, 0, sizeof(macro_list)); + pEntry = (macro_list *)calloc(1, sizeof(macro_list)); /* Very first part of the string is the macro name. * How long is it? */ iLength = macro_length(pString); - pEntry->m_pMacro = (char *)malloc(iLength + 1); - memset(pEntry->m_pMacro, 0, iLength + 1); + pEntry->m_pMacro = (char *)calloc(iLength + 1, 1); strncpy(pEntry->m_pMacro, pString, iLength); /* Skip to the values. @@ -803,11 +801,9 @@ void add_values(char *pString, char_list **ppList) /* Allocate a new list entry for the next value. */ - pEntry = (char_list *)malloc(sizeof(char_list)); - memset(pEntry, 0, sizeof(char_list)); + pEntry = (char_list *)calloc(1, sizeof(char_list)); - pEntry->m_pString = (char *)malloc(iLength + 1); - memset(pEntry->m_pString, 0, iLength + 1); + pEntry->m_pString = (char *)calloc(iLength + 1, 1); strncpy(pEntry->m_pString, pString, iLength); /* Add new value entry to the end of the list. diff --git a/dbm/src/nsres.c b/dbm/src/nsres.c index e383d58e5c6..e69de29bb2d 100644 --- a/dbm/src/nsres.c +++ b/dbm/src/nsres.c @@ -1,307 +0,0 @@ -#include "watcomfx.h" - -#include "nsres.h" - -#include - -#include - -#include - -struct RESDATABASE -{ - DB *hdb; - NSRESTHREADINFO *threadinfo; - char * pbuf[MAXBUFNUM]; -} ; -typedef struct RESDATABASE * RESHANDLE; - -typedef struct STRINGDATA -{ - char *str; - unsigned int charsetid; -} STRINGDATA; - - -typedef unsigned int CHARSETTYPE; -#define RES_LOCK if (hres->threadinfo) hres->threadinfo->fn_lock(hres->threadinfo->lock); -#define RES_UNLOCK if (hres->threadinfo) hres->threadinfo->fn_unlock(hres->threadinfo->lock); - -int GenKeyData(const char *library, int32 id, DBT *key); - -/* - Right now, the page size used for resource is same as for Navigator cache - database - */ -HASHINFO res_hash_info = { - 32*1024, - 0, - 0, - 0, - 0, /* 64 * 1024U */ - 0}; - -int GenKeyData(const char *library, int32 id, DBT *key) -{ - char idstr[10]; - static char * strdata = NULL; - size_t len; - - if (strdata) - free (strdata); - - if (id == 0) - idstr[0] = '\0'; - else - { - sprintf(idstr, "%d", id); - /* itoa(id, idstr, 10); */ - } - - if (library == NULL) - len = strlen(idstr) + 1; - else - len = strlen(library) + strlen(idstr) + 1; - strdata = (char *) malloc (len); - strcpy(strdata, library); - strcat(strdata, idstr); - - key->size = len; - key->data = strdata; - - return 1; -} - -NSRESHANDLE NSResCreateTable(const char *filename, NSRESTHREADINFO *threadinfo) -{ - RESHANDLE hres; - int flag; - - flag = O_RDWR | O_CREAT; - - hres = (RESHANDLE) malloc ( sizeof(struct RESDATABASE) ); - memset(hres, 0, sizeof(struct RESDATABASE)); - - if (threadinfo && threadinfo->lock && threadinfo->fn_lock - && threadinfo->fn_unlock) - { - hres->threadinfo = (NSRESTHREADINFO *) malloc( sizeof(NSRESTHREADINFO) ); - hres->threadinfo->lock = threadinfo->lock; - hres->threadinfo->fn_lock = threadinfo->fn_lock; - hres->threadinfo->fn_unlock = threadinfo->fn_unlock; - } - - - RES_LOCK - - hres->hdb = dbopen(filename, flag, 0644, DB_HASH, &res_hash_info); - - RES_UNLOCK - - if(!hres->hdb) - return NULL; - - return (NSRESHANDLE) hres; -} - -NSRESHANDLE NSResOpenTable(const char *filename, NSRESTHREADINFO *threadinfo) -{ - RESHANDLE hres; - int flag; - - flag = O_RDONLY; /* only open database for reading */ - - hres = (RESHANDLE) malloc ( sizeof(struct RESDATABASE) ); - memset(hres, 0, sizeof(struct RESDATABASE)); - - if (threadinfo && threadinfo->lock && threadinfo->fn_lock - && threadinfo->fn_unlock) - { - hres->threadinfo = (NSRESTHREADINFO *) malloc( sizeof(NSRESTHREADINFO) ); - hres->threadinfo->lock = threadinfo->lock; - hres->threadinfo->fn_lock = threadinfo->fn_lock; - hres->threadinfo->fn_unlock = threadinfo->fn_unlock; - } - - - RES_LOCK - - hres->hdb = dbopen(filename, flag, 0644, DB_HASH, &res_hash_info); - - RES_UNLOCK - - if(!hres->hdb) - return NULL; - - return (NSRESHANDLE) hres; -} - - - -void NSResCloseTable(NSRESHANDLE handle) -{ - RESHANDLE hres; - int i; - - if (handle == NULL) - return; - hres = (RESHANDLE) handle; - - RES_LOCK - - (*hres->hdb->sync)(hres->hdb, 0); - (*hres->hdb->close)(hres->hdb); - - RES_UNLOCK - - for (i = 0; i < MAXBUFNUM; i++) - { - if (hres->pbuf[i]) - free (hres->pbuf[i]); - } - - if (hres->threadinfo) - free (hres->threadinfo); - free (hres); -} - - -char *NSResLoadString(NSRESHANDLE handle, const char * library, int32 id, - unsigned int charsetid, char *retbuf) -{ - int status; - RESHANDLE hres; - DBT key, data; - if (handle == NULL) - return NULL; - - hres = (RESHANDLE) handle; - GenKeyData(library, id, &key); - - RES_LOCK - - status = (*hres->hdb->get)(hres->hdb, &key, &data, 0); - - RES_UNLOCK - - if (retbuf) - { - memcpy(retbuf, (char *)data.data + sizeof(CHARSETTYPE), data.size - sizeof(CHARSETTYPE)); - return retbuf; - } - else - { - static int WhichString = 0; - static int bFirstTime = 1; - char *szLoadedString; - int i; - - RES_LOCK - - if (bFirstTime) { - for (i = 0; i < MAXBUFNUM; i++) - hres->pbuf[i] = (char *) malloc(MAXSTRINGLEN * sizeof(char)); - bFirstTime = 0; - } - - szLoadedString = hres->pbuf[WhichString]; - WhichString++; - - /* reset to 0, if WhichString reaches to the end */ - if (WhichString == MAXBUFNUM) - WhichString = 0; - - if (status == 0) - memcpy(szLoadedString, (char *) data.data + sizeof(CHARSETTYPE), - data.size - sizeof(CHARSETTYPE)); - else - szLoadedString[0] = 0; - - RES_UNLOCK - - return szLoadedString; - } -} - -int32 NSResGetSize(NSRESHANDLE handle, const char *library, int32 id) -{ - int status; - RESHANDLE hres; - DBT key, data; - if (handle == NULL) - return 0; - hres = (RESHANDLE) handle; - GenKeyData(library, id, &key); - - RES_LOCK - - status = (*hres->hdb->get)(hres->hdb, &key, &data, 0); - - RES_UNLOCK - - return data.size - sizeof(CHARSETTYPE); -} - -int32 NSResLoadResource(NSRESHANDLE handle, const char *library, int32 id, char *retbuf) -{ - int status; - RESHANDLE hres; - DBT key, data; - if (handle == NULL) - return 0; - hres = (RESHANDLE) handle; - GenKeyData(library, id, &key); - - RES_LOCK - - status = (*hres->hdb->get)(hres->hdb, &key, &data, 0); - - RES_UNLOCK - - if (retbuf) - { - memcpy(retbuf, (char *)data.data + sizeof(CHARSETTYPE), data.size - sizeof(CHARSETTYPE)); - return data.size; - } - else - return 0; -} - -int NSResAddString(NSRESHANDLE handle, const char *library, int32 id, - const char *string, unsigned int charset) -{ - int status; - RESHANDLE hres; - DBT key, data; - char * recdata; - - if (handle == NULL) - return 0; - hres = (RESHANDLE) handle; - - GenKeyData(library, id, &key); - - data.size = sizeof(CHARSETTYPE) + (strlen(string) + 1) ; - - recdata = (char *) malloc(data.size) ; - - /* set charset to the first field of record data */ - *((CHARSETTYPE *)recdata) = (CHARSETTYPE)charset; - - /* set data field */ - memcpy(recdata+sizeof(CHARSETTYPE), string, strlen(string) + 1); - - data.data = recdata; - - RES_LOCK - - status = (*hres->hdb->put)(hres->hdb, &key, &data, 0); - - - if (recdata) - free(recdata); - - RES_UNLOCK - - return status; -} diff --git a/embedding/browser/activex/src/pluginhostctrl/npn.cpp b/embedding/browser/activex/src/pluginhostctrl/npn.cpp index c7592283652..665e2f066ab 100644 --- a/embedding/browser/activex/src/pluginhostctrl/npn.cpp +++ b/embedding/browser/activex/src/pluginhostctrl/npn.cpp @@ -104,8 +104,7 @@ _OpenURL(NPP npp, const char *szURL, const char *szTarget, void *pNotifyData, co USES_CONVERSION; DWORD cbNewURL = (url.Length() + bstrCurrentURL.Length() + 1) * sizeof(WCHAR); DWORD cbNewURLUsed = 0; - WCHAR *pszNewURL = (WCHAR *) malloc(cbNewURL); - memset(pszNewURL, 0, cbNewURL); + WCHAR *pszNewURL = (WCHAR *) calloc(cbNewURL, 1); ATLASSERT(pszNewURL); CoInternetCombineUrl( diff --git a/gfx/src/xlibrgb/xlibrgb.c b/gfx/src/xlibrgb/xlibrgb.c index ff21e6732e6..17473b109fc 100644 --- a/gfx/src/xlibrgb/xlibrgb.c +++ b/gfx/src/xlibrgb/xlibrgb.c @@ -437,8 +437,7 @@ xxlib_rgb_make_colorcube (XlibRgbHandle *handle, unsigned long *pixels, int nr, unsigned char rt[16], gt[16], bt[16]; int i; - handle->colorcube = malloc(sizeof(unsigned char) * 4096); - memset(handle->colorcube, 0, (sizeof(unsigned char) * 4096)); + handle->colorcube = calloc(4096, 1); for (i = 0; i < 16; i++) { rt[i] = ng * nb * ((i * 17 * (nr - 1) + 128) >> 8); @@ -462,8 +461,7 @@ xxlib_rgb_make_colorcube_d (XlibRgbHandle *handle, unsigned long *pixels, int nr int r, g, b; int i; - handle->colorcube_d = malloc(sizeof(unsigned char) * 512); - memset(handle->colorcube_d, 0, (sizeof(unsigned char) * 512)); + handle->colorcube_d = calloc(512, 1); for (i = 0; i < 512; i++) { r = MIN (nr - 1, i >> 6); diff --git a/mailnews/mime/src/mimefilt.cpp b/mailnews/mime/src/mimefilt.cpp index aed80762835..991f757a61c 100644 --- a/mailnews/mime/src/mimefilt.cpp +++ b/mailnews/mime/src/mimefilt.cpp @@ -374,8 +374,7 @@ main (int argc, char **argv) PR_Init("mimefilt", 24, 1, 0); - cdb_handle = (CERTCertDBHandle *) malloc(sizeof(*cdb_handle)); - memset(cdb_handle, 0, sizeof(*cdb_handle)); + cdb_handle = (CERTCertDBHandle *) calloc(1, sizeof(*cdb_handle)); if (SECSuccess != CERT_OpenCertDB(cdb_handle, PR_FALSE, test_cdb_name_cb, NULL)) CERT_OpenVolatileCertDB(cdb_handle); diff --git a/rdf/opendir/genopendir.c b/rdf/opendir/genopendir.c index 76b6a89fe43..efdeb7befce 100644 --- a/rdf/opendir/genopendir.c +++ b/rdf/opendir/genopendir.c @@ -117,8 +117,7 @@ Init () { gDesc = RDF_GetResource("Description", 1); gEditor = RDF_GetResource("editor", 1); gNewsGroup = RDF_GetResource("newsGroup", 1); - gTemplate = (char*) malloc(MAX_TEMPLATE_SIZE+1); - memset(gTemplate, '\0', MAX_TEMPLATE_SIZE); + gTemplate = (char*) calloc(MAX_TEMPLATE_SIZE+1, 1); n = fread(gTemplate, 1, MAX_TEMPLATE_SIZE, templateFile); gTemplate[n] = '\0'; fclose(templateFile); @@ -226,19 +225,6 @@ void describeCategory (WriteClientProc callBack, void* obj, RDF_Resource u) { #define MRN 1000 -char* -xGetMem (size_t n) { - char* ans = (char*) malloc(n); - if (ans) memset(ans, '\0', n); - return ans; -} - - -void -xFreeMem(void* item) { - free(item); -} - RDF_Resource stToProp (char c) { if (!c || (c == 'a')) { @@ -260,8 +246,8 @@ AnswerSearchQuery (WriteClientProc callBack, void* obj, char *query, char* cooki size_t catn = 0; size_t itemn = 0; size_t index; - RDF_Resource* cats = (RDF_Resource*)xGetMem(sizeof(RDF_Resource) * (MRN + 1)); - RDF_Resource* items = (RDF_Resource*)xGetMem(sizeof(RDF_Resource) * (MRN + 1)); + RDF_Resource* cats = (RDF_Resource*)calloc(MRN+1, sizeof(RDF_Resource)); + RDF_Resource* items = (RDF_Resource*)calloc(MRN+1, sizeof(RDF_Resource)); char showDesc = cookiePropValue(cookie, "searchDesc"); (*callBack)(obj, PREFIX); @@ -302,8 +288,8 @@ AnswerSearchQuery (WriteClientProc callBack, void* obj, char *query, char* cooki (*callBack)(obj, ""); } - xFreeMem(items); - xFreeMem(cats); + free(items); + free(cats); } diff --git a/rdf/opendir/rdfparse.c b/rdf/opendir/rdfparse.c index cca9164da0f..a5797019be6 100644 --- a/rdf/opendir/rdfparse.c +++ b/rdf/opendir/rdfparse.c @@ -178,9 +178,7 @@ startsWith (const char* pattern, const char* uuid) { char* getMem (size_t n) { - char* ans = (char*) malloc(n); - if (ans) memset(ans, '\0', n); - return ans; + return (char*) calloc(1, n); } void diff --git a/rdf/opendir/spf2ldiff.c b/rdf/opendir/spf2ldiff.c index aea5425aa05..583d6c97148 100644 --- a/rdf/opendir/spf2ldiff.c +++ b/rdf/opendir/spf2ldiff.c @@ -119,9 +119,7 @@ startsWith (const char* pattern, const char* uuid) char* getMem (size_t n) { - char* ans = (char*) malloc(n); - if (ans) memset(ans, '\0', n); - return ans; + return (char*) calloc(1, n); } void diff --git a/rdf/opendir/test.c b/rdf/opendir/test.c index 8f03999c3ea..631fcf4b4bc 100644 --- a/rdf/opendir/test.c +++ b/rdf/opendir/test.c @@ -43,8 +43,7 @@ #include void main () { - char* nextQuery = malloc(200); - memset(nextQuery, '\0', 200); + char* nextQuery = calloc(200, sizeof(char)); RDF_Initialize(); while (1) { int n; diff --git a/widget/src/os2/nsFilePicker.cpp b/widget/src/os2/nsFilePicker.cpp index 6247de8de25..ad0f0ba01c7 100644 --- a/widget/src/os2/nsFilePicker.cpp +++ b/widget/src/os2/nsFilePicker.cpp @@ -156,8 +156,7 @@ NS_IMETHODIMP nsFilePicker::Show(PRInt16 *retval) filedlg.fl |= FDS_OPEN_DIALOG; } PMYDATA pmydata; - pmydata = (PMYDATA)malloc(sizeof(MYDATA)); - memset(pmydata, 0, sizeof(MYDATA)); + pmydata = (PMYDATA)calloc(1, sizeof(MYDATA)); filedlg.ulUser = (ULONG)pmydata; filedlg.pfnDlgProc = FileDialogProc; diff --git a/xpinstall/wizard/unix/src2/nsXIEngine.cpp b/xpinstall/wizard/unix/src2/nsXIEngine.cpp index 1296243b6e5..bc5c4ea2798 100644 --- a/xpinstall/wizard/unix/src2/nsXIEngine.cpp +++ b/xpinstall/wizard/unix/src2/nsXIEngine.cpp @@ -579,8 +579,7 @@ nsXIEngine::ParseURL(char *aURL, char **aHost, char **aDir) hostTerminator = strchr(host, '/'); if (!hostTerminator) return E_BAD_FTP_URL; - *aHost = (char *) malloc(sizeof(char) * (hostTerminator - host + 1)); - memset(*aHost, 0, (hostTerminator - host + 1)); + *aHost = (char *) calloc(hostTerminator - host + 1, 1); strncpy(*aHost, host, hostTerminator - host); dirTerminator = strrchr(hostTerminator + 1, '/'); diff --git a/xpinstall/wizard/windows/ds32/ds32.cpp b/xpinstall/wizard/windows/ds32/ds32.cpp index f5a8464f4da..a282bec58ea 100644 --- a/xpinstall/wizard/windows/ds32/ds32.cpp +++ b/xpinstall/wizard/windows/ds32/ds32.cpp @@ -392,8 +392,7 @@ int ParseArgumentList(struct Params *ArgList, int argc, char *argv[]) ArgList->Sector = -1; ArgList->SuppressHeader = 0; ArgList->AutoInfoFormat = 0; - ArgList->DirName = (char *)malloc(sizeof(char)*MAX_BUF); - memset(ArgList->DirName, 0, MAX_BUF); + ArgList->DirName = (char *)calloc(MAX_BUF, 1); for(i = 1; i < argc; i++) {