Skip to content

Commit

Permalink
Tweaks to alloc error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Sep 8, 2024
1 parent 06492b9 commit b0e0111
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/xlookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ XLookupTable *xAllocLookup(unsigned int size) {

p = (XLookupPrivate *) calloc(1, sizeof(XLookupPrivate));
if(!p) {
x_error(0, errno, fn, "calloc() error");
return NULL;
perror("ERROR! alloc error");
exit(errno);
}

p->table = (XLookupEntry **) calloc(n, sizeof(XLookupEntry *));
Expand All @@ -350,8 +350,8 @@ XLookupTable *xAllocLookup(unsigned int size) {

tab = (XLookupTable *) calloc(1, sizeof(XLookupTable));
if(!tab) {
x_error(0, errno, fn, "calloc() error");
return NULL;
perror("ERROR! alloc error");
exit(errno);
}

tab->priv = p;
Expand Down
17 changes: 10 additions & 7 deletions src/xstruct.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
*/
XStructure *xCreateStruct() {
XStructure *s = (XStructure *) calloc(1, sizeof(XStructure));
if(!s) x_error(0, errno, "xCreateStruct", "calloc() error");
if(!s) {
perror("ERROR! alloc error");
exit(errno);
}
return s;
}

Expand Down Expand Up @@ -104,8 +107,8 @@ XField *xCopyOfField(const XField *f) {

copy = (XField *) malloc(sizeof(XField));
if(!copy) {
x_error(0, errno, fn, "malloc() error");
return NULL;
perror("ERROR! alloc error");
exit(errno);
}

// Start with a clone...
Expand All @@ -132,7 +135,7 @@ XField *xCopyOfField(const XField *f) {

c = calloc(eCount, sizeof(XStructure));
if(!c) {
x_error(0, errno, fn, "calloc() error (eCount=%d)", eCount);
x_error(0, errno, fn, "calloc() error (%d XStructure)", eCount);
xDestroyField(copy);
return NULL;
}
Expand Down Expand Up @@ -788,11 +791,11 @@ int xReduceDims(int *ndim, int *sizes) {

int i;

if(!ndim) return x_error(X_SIZE_INVALID, EINVAL, fn, "ndim pointer is NULL");
if(ndim == NULL) return x_error(X_SIZE_INVALID, EINVAL, fn, "ndim pointer is NULL");

if(*ndim <= 0) return X_SUCCESS;

if(sizes == NULL) return x_error(X_SIZE_INVALID, EINVAL, fn, "sizes is NULL (ndim = %d)", ndim);
if(sizes == NULL) return x_error(X_SIZE_INVALID, EINVAL, fn, "sizes is NULL (ndim = %d)", *ndim);

for(i = *ndim; --i >= 0; ) if (sizes[i] == 0) {
*ndim = 1;
Expand Down Expand Up @@ -855,7 +858,7 @@ int xReduceAllDims(XStructure *s) {
while(--i >= 0) {
int status = xReduceAllDims(&sub[i]);
if(status < 0) {
char *id = malloc(strlen(f->name + 20));
char *id = malloc(strlen(f->name) + 20);
sprintf(id, "%s[%d]", f->name, i);
x_trace(fn, id, status);
free(id);
Expand Down

0 comments on commit b0e0111

Please sign in to comment.