From 4b06a70b5fb42334ec66875a6cf33a482975fb98 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 | 6 ++--- .../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, 21 insertions(+), 53 deletions(-) diff --git a/config/mantomak.c b/config/mantomak.c index 8ae22e0b87e9..ac8acbd5883f 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 e383d58e5c61..4b4dae4a226c 100644 --- a/dbm/src/nsres.c +++ b/dbm/src/nsres.c @@ -79,8 +79,7 @@ NSRESHANDLE NSResCreateTable(const char *filename, NSRESTHREADINFO *threadinfo) flag = O_RDWR | O_CREAT; - hres = (RESHANDLE) malloc ( sizeof(struct RESDATABASE) ); - memset(hres, 0, sizeof(struct RESDATABASE)); + hres = (RESHANDLE) calloc ( 1, sizeof(struct RESDATABASE) ); if (threadinfo && threadinfo->lock && threadinfo->fn_lock && threadinfo->fn_unlock) @@ -111,8 +110,7 @@ NSRESHANDLE NSResOpenTable(const char *filename, NSRESTHREADINFO *threadinfo) flag = O_RDONLY; /* only open database for reading */ - hres = (RESHANDLE) malloc ( sizeof(struct RESDATABASE) ); - memset(hres, 0, sizeof(struct RESDATABASE)); + hres = (RESHANDLE) calloc ( 1, sizeof(struct RESDATABASE) ); if (threadinfo && threadinfo->lock && threadinfo->fn_lock && threadinfo->fn_unlock) diff --git a/embedding/browser/activex/src/pluginhostctrl/npn.cpp b/embedding/browser/activex/src/pluginhostctrl/npn.cpp index c7592283652f..665e2f066ab1 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 ff21e6732e6d..17473b109fc7 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 aed807628359..991f757a61c0 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 76b6a89fe43d..efdeb7befce9 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 cca9164da0fa..a5797019be60 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 aea5425aa05c..583d6c971481 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 8f03999c3eab..631fcf4b4bce 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 6247de8de256..ad0f0ba01c75 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 1296243b6e5e..bc5c4ea27988 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 f5a8464f4da4..a282bec58ea4 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++) {