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) ...@@ -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, void *cdiXmalloc(size_t size, const char *filename, const char *functionname,
int line) int line)
{ {
void * value = calloc (1, size ); void *value = malloc(size);
if ( value == NULL ) if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "malloc failed: %s", cdiAbort(filename, functionname, line, "malloc failed: %s",
strerror(errno)); strerror(errno));
return value; return value;
...@@ -537,8 +537,8 @@ void *cdiXmalloc(size_t size, const char *filename, const char *functionname, ...@@ -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, void *cdiXcalloc(size_t nmemb, size_t size, const char *filename,
const char *functionname, int line) const char *functionname, int line)
{ {
void * value = calloc ( nmemb, size ); void *value = calloc(nmemb, size);
if ( value == NULL ) if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "calloc failed: %s", cdiAbort(filename, functionname, line, "calloc failed: %s",
strerror(errno) ); strerror(errno) );
return value; return value;
...@@ -548,7 +548,7 @@ void *cdiXrealloc(void *p, size_t size, const char *functionname, ...@@ -548,7 +548,7 @@ void *cdiXrealloc(void *p, size_t size, const char *functionname,
const char *filename, int line) const char *filename, int line)
{ {
void *value = realloc(p, size); void *value = realloc(p, size);
if ( value == NULL ) if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "realloc failed: %s", cdiAbort(filename, functionname, line, "realloc failed: %s",
strerror(errno)); strerror(errno));
return value; return value;
......
...@@ -83,7 +83,11 @@ void grid_init(grid_t *gridptr) ...@@ -83,7 +83,11 @@ void grid_init(grid_t *gridptr)
gridptr->ybounds = NULL; gridptr->ybounds = NULL;
gridptr->rowlon = NULL; gridptr->rowlon = NULL;
gridptr->nrowlon = 0; gridptr->nrowlon = 0;
gridptr->xfirst = 0.0;
gridptr->xlast = 0.0;
gridptr->xinc = 0.0; gridptr->xinc = 0.0;
gridptr->yfirst = 0.0;
gridptr->ylast = 0.0;
gridptr->yinc = 0.0; gridptr->yinc = 0.0;
gridptr->lcc_originLon = 0.0; gridptr->lcc_originLon = 0.0;
gridptr->lcc_originLat = 0.0; gridptr->lcc_originLat = 0.0;
......
...@@ -97,7 +97,7 @@ void pioInfoInit ( pioInfo_t * p ) ...@@ -97,7 +97,7 @@ void pioInfoInit ( pioInfo_t * p )
void commInit ( void ) void commInit ( void )
{ {
xassert ( info == 0 ); xassert ( info == 0 );
info = (pioInfo_t*) xmalloc ( sizeof ( pioInfo_t )); info = xcalloc(1, sizeof (pioInfo_t));
pioInfoInit ( info ); pioInfoInit ( info );
} }
...@@ -592,8 +592,8 @@ void commEvalPhysNodes ( void ) ...@@ -592,8 +592,8 @@ void commEvalPhysNodes ( void )
// define nodeSizes // define nodeSizes
info->nodeInfo.nNodes = nNodes; info->nodeInfo.nNodes = nNodes;
info->nodeSizes = (int *)xmalloc((size_t)info->nodeInfo.nNodes info->nodeSizes = xcalloc((size_t)info->nodeInfo.nNodes,
* sizeof (info->nodeSizes[0])); sizeof (info->nodeSizes[0]));
collID = 0; collID = 0;
for ( IOID = 0; IOID < info->nProcsIO; IOID++ ) for ( IOID = 0; IOID < info->nProcsIO; IOID++ )
if ( nodeInfo[IOID].isProcColl ) if ( nodeInfo[IOID].isProcColl )
......
...@@ -267,7 +267,7 @@ varsMapNDeco(int nNodes, int *nodeSizes) ...@@ -267,7 +267,7 @@ varsMapNDeco(int nNodes, int *nodeSizes)
streamSizes[i] = streamInqNvars ( * ( resHs + i )); streamSizes[i] = streamInqNvars ( * ( resHs + i ));
nVars = sum_int((size_t)nStreams, streamSizes); 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])); varMapping = xmalloc((size_t)nVars * sizeof (varMapping[0]));
for (int i = 0; i < nStreams; i++ ) for (int i = 0; i < nStreams; i++ )
...@@ -455,7 +455,7 @@ void modelWinCreate ( void ) ...@@ -455,7 +455,7 @@ void modelWinCreate ( void )
int nProcsColl = commInqNProcsColl (); int nProcsColl = commInqNProcsColl ();
xdebug("%s", "START"); xdebug("%s", "START");
txWin = xmalloc((size_t)nProcsColl * sizeof (txWin[0])); txWin = xcalloc((size_t)nProcsColl, sizeof (txWin[0]));
modelWinDefBufferSizes (); modelWinDefBufferSizes ();
ranks[0] = commInqNProcsModel (); ranks[0] = commInqNProcsModel ();
......
...@@ -253,7 +253,7 @@ void pioWriterAIO(void) ...@@ -253,7 +253,7 @@ void pioWriterAIO(void)
xdebug ( "nProcsCollNode=%d on this node", nProcsCollNode ); xdebug ( "nProcsCollNode=%d on this node", nProcsCollNode );
bibBFiledataPA = listSetNew(destroyBFiledataPA, compareNamesBPA); bibBFiledataPA = listSetNew(destroyBFiledataPA, compareNamesBPA);
sentFinalize = xmalloc((size_t)nProcsCollNode * sizeof (sentFinalize[0])); sentFinalize = xcalloc((size_t)nProcsCollNode, sizeof (sentFinalize[0]));
for ( ;; ) for ( ;; )
{ {
......
...@@ -176,7 +176,7 @@ void fpgPOSIXFPGUARDSENDRECV ( void ) ...@@ -176,7 +176,7 @@ void fpgPOSIXFPGUARDSENDRECV ( void )
xdebug ( "ncollectors=%d on this node", nProcsCollNode ); xdebug ( "ncollectors=%d on this node", nProcsCollNode );
bibBFiledataPF = listSetNew( destroyBFiledataPF, fileIDCmpB); bibBFiledataPF = listSetNew( destroyBFiledataPF, fileIDCmpB);
sentFinalize = xmalloc((size_t)nProcsCollNode * sizeof (sentFinalize[0])); sentFinalize = xcalloc((size_t)nProcsCollNode, sizeof (sentFinalize[0]));
for ( ;; ) for ( ;; )
{ {
......
...@@ -141,7 +141,7 @@ pioWriterStdIO(void) ...@@ -141,7 +141,7 @@ pioWriterStdIO(void)
xdebug ( "ncollectors=%d on this node", nProcsCollNode ); xdebug ( "ncollectors=%d on this node", nProcsCollNode );
bibBFiledataP = listSetNew(destroyBFiledataP, compareNamesBP); bibBFiledataP = listSetNew(destroyBFiledataP, compareNamesBP);
sentFinalize = xmalloc((size_t)nProcsCollNode * sizeof (sentFinalize[0])); sentFinalize = xcalloc((size_t)nProcsCollNode, sizeof (sentFinalize[0]));
for ( ;; ) 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