Skip to content

Commit

Permalink
libgis: close files if G_copy_file fails (#2266)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti authored and nilason committed Mar 25, 2022
1 parent 982167a commit 3876e7f
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/gis/copy_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,27 @@ int G_copy_file(const char *infile, const char *outfile)

infp = fopen(infile, "r");
if (infp == NULL) {
G_warning("Cannot open %s for reading: %s", infile, strerror(errno));
return 0;
G_warning("Cannot open %s for reading: %s", infile, strerror(errno));
return 0;
}

outfp = fopen(outfile, "w");
if (outfp == NULL) {
G_warning("Cannot open %s for writing: %s", outfile, strerror(errno));
return 0;
G_warning("Cannot open %s for writing: %s", outfile, strerror(errno));
fclose(infp);
return 0;
}

while ((inchar = getc(infp)) != EOF) {
/* Read a character at a time from infile until EOF
* and copy to outfile */
outchar = putc(inchar, outfp);
if (outchar != inchar) {
G_warning("Error writing to %s", outfile);
return 0;
}
/* Read a character at a time from infile until EOF
* and copy to outfile */
outchar = putc(inchar, outfp);
if (outchar != inchar) {
G_warning("Error writing to %s", outfile);
fclose(infp);
fclose(outfp);
return 0;
}
}
fflush(outfp);

Expand Down

0 comments on commit 3876e7f

Please sign in to comment.