Skip to content

Commit

Permalink
Check for errors when closing files
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Apr 5, 2016
1 parent c0a0aef commit e394501
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
56 changes: 45 additions & 11 deletions geojson.c
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ void *run_read_parallel(void *v) {
}
if (fclose(a->fp) != 0) {
perror("close source file");
exit(EXIT_FAILURE);
}

*(a->is_parsing) = 0;
Expand Down Expand Up @@ -1216,8 +1217,14 @@ void radix1(int *geomfds_in, int *indexfds_in, int inputs, int prefix, int split
}

for (i = 0; i < splits; i++) {
fclose(geomfiles[i]);
fclose(indexfiles[i]);
if (fclose(geomfiles[i]) != 0) {
perror("fclose geom");
exit(EXIT_FAILURE);
}
if (fclose(indexfiles[i]) != 0) {
perror("fclose index");
exit(EXIT_FAILURE);
}

availfiles += 2;
}
Expand Down Expand Up @@ -1637,7 +1644,10 @@ int read_json(int argc, struct source **sourcelist, char *fname, const char *lay
FILE *fp = fdopen(fd, "r");
if (fp == NULL) {
perror(sourcelist[source]->file);
close(fd);
if (close(fd) != 0) {
perror("close source file");
exit(EXIT_FAILURE);
}
continue;
}

Expand Down Expand Up @@ -1733,7 +1743,10 @@ int read_json(int argc, struct source **sourcelist, char *fname, const char *lay
overall_offset = layer_seq;
}

fclose(fp);
if (fclose(fp) != 0) {
perror("fclose input");
exit(EXIT_FAILURE);
}
}
}

Expand All @@ -1743,9 +1756,18 @@ int read_json(int argc, struct source **sourcelist, char *fname, const char *lay
}

for (i = 0; i < CPUS; i++) {
fclose(reader[i].metafile);
fclose(reader[i].geomfile);
fclose(reader[i].indexfile);
if (fclose(reader[i].metafile) != 0) {
perror("fclose meta");
exit(EXIT_FAILURE);
}
if (fclose(reader[i].geomfile) != 0) {
perror("fclose geom");
exit(EXIT_FAILURE);
}
if (fclose(reader[i].indexfile) != 0) {
perror("fclose index");
exit(EXIT_FAILURE);
}
memfile_close(reader[i].treefile);

if (fstat(reader[i].geomfd, &reader[i].geomst) != 0) {
Expand Down Expand Up @@ -1901,8 +1923,14 @@ int read_json(int argc, struct source **sourcelist, char *fname, const char *lay
memfile_close(reader[i].poolfile);
}

fclose(poolfile);
fclose(metafile);
if (fclose(poolfile) != 0) {
perror("fclose pool");
exit(EXIT_FAILURE);
}
if (fclose(metafile) != 0) {
perror("fclose meta");
exit(EXIT_FAILURE);
}

char *meta = (char *) mmap(NULL, metapos, PROT_READ, MAP_PRIVATE, metafd, 0);
if (meta == MAP_FAILED) {
Expand Down Expand Up @@ -1964,8 +1992,14 @@ int read_json(int argc, struct source **sourcelist, char *fname, const char *lay
/* end of tile */
serialize_byte(geomfile, -2, &geompos, fname);

fclose(geomfile);
fclose(indexfile);
if (fclose(geomfile) != 0) {
perror("fclose geom");
exit(EXIT_FAILURE);
}
if (fclose(indexfile) != 0) {
perror("fclose index");
exit(EXIT_FAILURE);
}

struct stat indexst;
if (fstat(indexfd, &indexst) < 0) {
Expand Down
13 changes: 11 additions & 2 deletions tile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1316,8 +1316,17 @@ int traverse_zooms(int *geomfd, off_t *geom_size, char *metabase, char *stringpo
}

for (j = 0; j < TEMP_FILES; j++) {
close(geomfd[j]);
fclose(sub[j]);
// Can be < 0 if there is only one source file, at z0
if (geomfd[j] >= 0) {
if (close(geomfd[j]) != 0) {
perror("close geom");
exit(EXIT_FAILURE);
}
}
if (fclose(sub[j]) != 0) {
perror("close subfile");
exit(EXIT_FAILURE);
}

struct stat geomst;
if (fstat(subfd[j], &geomst) != 0) {
Expand Down

0 comments on commit e394501

Please sign in to comment.