Browse Source

libgis: close files if G_copy_file fails (#2266)

Loïc Bartoletti 2 years ago
parent
commit
3587128a97
1 changed files with 14 additions and 11 deletions
  1. 14 11
      lib/gis/copy_file.c

+ 14 - 11
lib/gis/copy_file.c

@@ -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);