Skip to content

Commit

Permalink
Use calloc() instead of malloc() followed by memset to 0. Bug 124302,…
Browse files Browse the repository at this point in the history
… patch by

Aaron Lehmann <[email protected]>, r=timeless, sr=bzbarsky
  • Loading branch information
bzbarsky%mit.edu committed Nov 20, 2002
1 parent c265c34 commit 4b06a70
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 53 deletions.
12 changes: 4 additions & 8 deletions config/mantomak.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 2 additions & 4 deletions dbm/src/nsres.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions embedding/browser/activex/src/pluginhostctrl/npn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions gfx/src/xlibrgb/xlibrgb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions mailnews/mime/src/mimefilt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 5 additions & 19 deletions rdf/opendir/genopendir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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')) {
Expand All @@ -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);
Expand Down Expand Up @@ -302,8 +288,8 @@ AnswerSearchQuery (WriteClientProc callBack, void* obj, char *query, char* cooki
(*callBack)(obj, "</UL>");
}

xFreeMem(items);
xFreeMem(cats);
free(items);
free(cats);
}


Expand Down
4 changes: 1 addition & 3 deletions rdf/opendir/rdfparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions rdf/opendir/spf2ldiff.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions rdf/opendir/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
#include <stdio.h>

void main () {
char* nextQuery = malloc(200);
memset(nextQuery, '\0', 200);
char* nextQuery = calloc(200, sizeof(char));
RDF_Initialize();
while (1) {
int n;
Expand Down
3 changes: 1 addition & 2 deletions widget/src/os2/nsFilePicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions xpinstall/wizard/unix/src2/nsXIEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
Expand Down
3 changes: 1 addition & 2 deletions xpinstall/wizard/windows/ds32/ds32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand Down

0 comments on commit 4b06a70

Please sign in to comment.