Commit a6a5f1f6 authored by Thomas Jahns's avatar Thomas Jahns 🤸
Browse files

Fix incorrect handling of NULL pointer return values.

* malloc et al. can return NULL on zero-size allocations.
* this commit also reverts the dubious calling of calloc by xmalloc.
parent 50d55942
......@@ -527,8 +527,8 @@ void Free(const char *caller, const char *file, int line, void *ptr)
void *cdiXmalloc(size_t size, const char *filename, const char *functionname,
int line)
{
void * value = calloc (1, size );
if ( value == NULL )
void *value = malloc(size);
if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "malloc failed: %s",
strerror(errno));
return value;
......@@ -537,8 +537,8 @@ void *cdiXmalloc(size_t size, const char *filename, const char *functionname,
void *cdiXcalloc(size_t nmemb, size_t size, const char *filename,
const char *functionname, int line)
{
void * value = calloc ( nmemb, size );
if ( value == NULL )
void *value = calloc(nmemb, size);
if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "calloc failed: %s",
strerror(errno) );
return value;
......@@ -548,7 +548,7 @@ void *cdiXrealloc(void *p, size_t size, const char *functionname,
const char *filename, int line)
{
void *value = realloc(p, size);
if ( value == NULL )
if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "realloc failed: %s",
strerror(errno));
return value;
......
......@@ -83,7 +83,11 @@ void grid_init(grid_t *gridptr)
gridptr->ybounds = NULL;
gridptr->rowlon = NULL;
gridptr->nrowlon = 0;
gridptr->xfirst = 0.0;
gridptr->xlast = 0.0;
gridptr->xinc = 0.0;
gridptr->yfirst = 0.0;
gridptr->ylast = 0.0;
gridptr->yinc = 0.0;
gridptr->lcc_originLon = 0.0;
gridptr->lcc_originLat = 0.0;
......
......@@ -97,7 +97,7 @@ void pioInfoInit ( pioInfo_t * p )
void commInit ( void )
{
xassert ( info == 0 );
info = (pioInfo_t*) xmalloc ( sizeof ( pioInfo_t ));
info = xcalloc(1, sizeof (pioInfo_t));
pioInfoInit ( info );
}
......@@ -592,8 +592,8 @@ void commEvalPhysNodes ( void )
// define nodeSizes
info->nodeInfo.nNodes = nNodes;
info->nodeSizes = (int *)xmalloc((size_t)info->nodeInfo.nNodes
* sizeof (info->nodeSizes[0]));
info->nodeSizes = xcalloc((size_t)info->nodeInfo.nNodes,
sizeof (info->nodeSizes[0]));
collID = 0;
for ( IOID = 0; IOID < info->nProcsIO; IOID++ )
if ( nodeInfo[IOID].isProcColl )
......
......@@ -267,7 +267,7 @@ varsMapNDeco(int nNodes, int *nodeSizes)
streamSizes[i] = streamInqNvars ( * ( resHs + i ));
nVars = sum_int((size_t)nStreams, streamSizes);
varSizes = xmalloc((size_t)nVars * sizeof (varSizes[0]));
varSizes = xcalloc((size_t)nVars, sizeof (varSizes[0]));
varMapping = xmalloc((size_t)nVars * sizeof (varMapping[0]));
for (int i = 0; i < nStreams; i++ )
......@@ -455,7 +455,7 @@ void modelWinCreate ( void )
int nProcsColl = commInqNProcsColl ();
xdebug("%s", "START");
txWin = xmalloc((size_t)nProcsColl * sizeof (txWin[0]));
txWin = xcalloc((size_t)nProcsColl, sizeof (txWin[0]));
modelWinDefBufferSizes ();
ranks[0] = commInqNProcsModel ();
......
......@@ -253,7 +253,7 @@ void pioWriterAIO(void)
xdebug ( "nProcsCollNode=%d on this node", nProcsCollNode );
bibBFiledataPA = listSetNew(destroyBFiledataPA, compareNamesBPA);
sentFinalize = xmalloc((size_t)nProcsCollNode * sizeof (sentFinalize[0]));
sentFinalize = xcalloc((size_t)nProcsCollNode, sizeof (sentFinalize[0]));
for ( ;; )
{
......
......@@ -176,7 +176,7 @@ void fpgPOSIXFPGUARDSENDRECV ( void )
xdebug ( "ncollectors=%d on this node", nProcsCollNode );
bibBFiledataPF = listSetNew( destroyBFiledataPF, fileIDCmpB);
sentFinalize = xmalloc((size_t)nProcsCollNode * sizeof (sentFinalize[0]));
sentFinalize = xcalloc((size_t)nProcsCollNode, sizeof (sentFinalize[0]));
for ( ;; )
{
......
......@@ -141,7 +141,7 @@ pioWriterStdIO(void)
xdebug ( "ncollectors=%d on this node", nProcsCollNode );
bibBFiledataP = listSetNew(destroyBFiledataP, compareNamesBP);
sentFinalize = xmalloc((size_t)nProcsCollNode * sizeof (sentFinalize[0]));
sentFinalize = xcalloc((size_t)nProcsCollNode, sizeof (sentFinalize[0]));
for ( ;; )
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment