Skip to content

Commit 640e100

Browse files
committed
cdl_header_parser.c check return error
1 parent 1bca499 commit 640e100

File tree

1 file changed

+67
-9
lines changed

1 file changed

+67
-9
lines changed

src/dispatchers/cdl_header_parser.c

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ typedef long long MPI_Offset;
8484
break; \
8585
default: err_str = "Other unknown error"; \
8686
} \
87-
printf("Error at %s:%d : %s\n", __FILE__,__LINE__, err_str); \
87+
printf("Error in %s and %d : %s\n", __FILE__,__LINE__, err_str); \
8888
err = 1; \
8989
goto err_out; \
9090
} \
@@ -94,7 +94,8 @@ typedef long long MPI_Offset;
9494
#define LINE_SIZE 1024
9595

9696
#define ERR_FORMAT(msg) { \
97-
printf("Error line %d: input file format at %s\n", __LINE__,msg); \
97+
printf("Error in %s at %d: input file format at %s\n", \
98+
__func__,__LINE__,msg); \
9899
err = NC_ENOTNC; \
99100
goto err_out; \
100101
}
@@ -180,7 +181,8 @@ int get_dimid(CDL_header *header,
180181
return NC_NOERR;
181182
}
182183

183-
printf("Error: failed to find dim ID for %s\n", name);
184+
printf("Error in %s at %d: failed to find dim ID for %s\n",
185+
__func__,__LINE__,name);
184186
return NC_EBADDIM;
185187
}
186188

@@ -299,6 +301,11 @@ int parse_dims(char **bptr,
299301
size_t len = dims->nelems + DIM_ARRAY_GROWBY;
300302
dims->value = (CDL_dim*) realloc(dims->value,
301303
sizeof(CDL_dim) * len);
304+
if (dims->value == NULL) {
305+
printf("Error in %s at %d: fail to realloc of size %zd (%s)\n",
306+
__func__,__LINE__,sizeof(CDL_dim)*len,strerror(errno));
307+
return NC_ENOMEM;
308+
}
302309
}
303310
CDL_dim *dimp = &dims->value[dims->nelems];
304311
dimp->name = strdup(key);
@@ -378,6 +385,11 @@ int parse_attr_value(CDL_attr *attrp,
378385
strcpy(str, val);
379386

380387
attrp->value = (void*) malloc(sizeof(double) * nelems);
388+
if (attrp->value == NULL) {
389+
printf("Error in %s at %d: fail to malloc of size %zd (%s)\n",
390+
__func__,__LINE__,sizeof(double)*nelems,strerror(errno));
391+
return NC_ENOMEM;
392+
}
381393

382394
if (xtype == NC_INT) PARSE_ATTR(int, 0, atoi)
383395
else if (xtype == NC_BYTE) PARSE_ATTR(signed char, 1, atoi)
@@ -452,13 +464,23 @@ int parse_attr(char **bptr,
452464
size_t len = attrs->nelems + GATTR_ARRAY_GROWBY;
453465
attrs->value = (CDL_attr*) realloc(attrs->value,
454466
sizeof(CDL_attr) * len);
467+
if (attrs->value == NULL) {
468+
printf("Error in %s at %d: fail to realloc of size %zd (%s)\n",
469+
__func__,__LINE__,sizeof(CDL_attr)*len,strerror(errno));
470+
return NC_ENOMEM;
471+
}
455472
}
456473
}
457474
else {
458475
if (attrs->nelems % ATTR_ARRAY_GROWBY == 0) {
459476
size_t len = attrs->nelems + ATTR_ARRAY_GROWBY;
460477
attrs->value = (CDL_attr*) realloc(attrs->value,
461478
sizeof(CDL_attr) * len);
479+
if (attrs->value == NULL) {
480+
printf("Error in %s at %d: fail to realloc of size %zd (%s)\n",
481+
__func__,__LINE__,sizeof(CDL_attr)*len,strerror(errno));
482+
return NC_ENOMEM;
483+
}
462484
}
463485
}
464486
CDL_attr *attrp = &attrs->value[attrs->nelems];
@@ -519,6 +541,11 @@ int parse_vars(char **bptr,
519541
size_t len = vars->nelems + VAR_ARRAY_GROWBY;
520542
vars->value = (CDL_var*) realloc(vars->value,
521543
sizeof(CDL_var) * len);
544+
if (vars->value == NULL) {
545+
printf("Error in %s at %d: fail to realloc of size %zd (%s)\n",
546+
__func__,__LINE__,sizeof(CDL_var)*len,strerror(errno));
547+
return NC_ENOMEM;
548+
}
522549
}
523550
CDL_var *varp = &vars->value[vars->nelems];
524551
var_name = strtok(NULL, "( ");
@@ -549,6 +576,11 @@ int parse_vars(char **bptr,
549576
#endif
550577

551578
varp->dimids = (int*) malloc(sizeof(int) * varp->ndims);
579+
if (varp->dimids == NULL) {
580+
printf("Error in %s at %d: fail to malloc of size %zd (%s)\n",
581+
__func__,__LINE__,sizeof(int)*varp->ndims,strerror(errno));
582+
return NC_ENOMEM;
583+
}
552584
strcpy(str, val);
553585
key = strtok(str, ", ");
554586
int i = 0;
@@ -653,37 +685,63 @@ int cdl_hdr_open(const char *filename,
653685
FILE *fptr;
654686
char *fbuf, *bptr;
655687
int err, dim_sec, var_sec, gattr_sec;
688+
size_t rlen;
656689
CDL_header *header=NULL;
657690

658691
if (cdl_nfiles + 1 > NC_MAX_NFILES)
659692
return NC_ENFILE;
660693

661694
fptr = fopen(filename, "r");
662695
if (fptr == NULL) {
663-
printf("Error: fail to open file %s (%s)\n",
664-
filename,strerror(errno));
696+
printf("Error in %s at %d: fail to open file %s (%s)\n",
697+
__func__,__LINE__,filename,strerror(errno));
665698
return NC_ENOENT;
666699
}
667700
err = fseek(fptr, 0, SEEK_END);
668701
if (err < 0) {
669-
printf("Error: fail to fseek file %s (%s)\n",
670-
filename,strerror(errno));
702+
printf("Error in %s at %d: fail to fseek SEEK_END file %s (%s)\n",
703+
__func__,__LINE__,filename,strerror(errno));
671704
return NC_EFILE;
672705
}
673706

674707
/* read the entire file into a buffer */
675708
long file_size = ftell(fptr);
676709
fbuf = (char *) malloc(file_size);
710+
if (fbuf == NULL) {
711+
printf("Error in %s at %d: fail to malloc of size %zd (%s)\n",
712+
__func__,__LINE__,file_size,strerror(errno));
713+
return NC_ENOMEM;
714+
}
677715
err = fseek(fptr, 0, SEEK_SET);
678-
fread(fbuf, 1, file_size, fptr);
716+
if (err < 0) {
717+
printf("Error in %s at %d: fail to fseek SEEK_SET file %s (%s)\n",
718+
__func__,__LINE__,filename,strerror(errno));
719+
return NC_EFILE;
720+
}
721+
rlen = fread(fbuf, 1, file_size, fptr);
722+
if (rlen < 0) {
723+
printf("Error in %s at %d: fail to fread file %s (%s)\n",
724+
__func__,__LINE__,filename,strerror(errno));
725+
return NC_EFILE;
726+
}
679727
bptr = fbuf;
680-
fclose(fptr);
728+
err = fclose(fptr);
729+
if (err == EOF) {
730+
printf("Error in %s at %d: fail to fclose file %s (%s)\n",
731+
__func__,__LINE__,filename,strerror(errno));
732+
return NC_EFILE;
733+
}
681734

682735
/* check netCDF file signature */
683736
err = parse_signature(&bptr);
684737
if (err != NC_NOERR) goto err_out;
685738

686739
header = (CDL_header*) calloc(1, sizeof(CDL_header));
740+
if (header == NULL) {
741+
printf("Error in %s at %d: fail to calloc of size %zd (%s)\n",
742+
__func__,__LINE__,sizeof(CDL_header),strerror(errno));
743+
return NC_ENOMEM;
744+
}
687745

688746
dim_sec = var_sec = gattr_sec = 0;
689747

0 commit comments

Comments
 (0)