Skip to contentSkip to conversation

found a resource leak in file builtin-fast-export.c

PATCH In review Martin Ettl 6 participants 10 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hi,

i have checked the source base of git with the static code analyis tool cppcheck. It brougt up an issue in file 1.6.3.3/builtin-fast-export.c at line 447.

The tool printed the following waring:

[git-1.6.3.3/builtin-fast-export.c:447]: (error) Resource leak: f

I have attached a patch to resolve this.

Best regards

Ettl Martin

Signature · 2 lines
Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate
für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02
builtin-fast-export_new.c renamed from builtin-fast-export.c +2 -1
@@ -442,8 +442,9 @@ static void export_marks(char *file)
 		deco++;
 	}
 
-	if (ferror(f) || fclose(f))
+	if (ferror(f))
 		error("Unable to write marks file %s.", file);
+  	fclose(f);
 }
 
 static void import_marks(char *input_file)
#1 lore raw

Hi Martin

Quoted 2 lines · Martin Ettl

I have attached a patch to resolve this.

Please read Documentation/SubmittingPatches in the source tree. And use git to track git.git!

As for the actual patch:

builtin-fast-export_new.c static void export_marks(char *file) · line 447

Quoted 10 lines
builtin-fast-export_new.c renamed from builtin-fast-export.c +2 -1
@@ -442,8 +442,9 @@ static void export_marks(char *file)
 		deco++;
 	}
 
-	if (ferror(f) || fclose(f))
+	if (ferror(f))
 		error("Unable to write marks file %s.", file);
+  	fclose(f);

You no longer check the error returned by fclose(). This is important, because the FILE* API may buffer writes, and a write error may only become apparent when fclose() flushes the file.

Quoted 3 lines
builtin-fast-export_new.c renamed from builtin-fast-export.c +0 -0
 }
 
 static void import_marks(char *input_file)
Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#2 lore raw

Hi,

Quoted 8 lines · Thomas Rast
Quoted 2 lines · Martin Ettl

I have attached a patch to resolve this.

Please read Documentation/SubmittingPatches in the source tree. And use git to track git.git!

As for the actual patch:

Thanks for inlining it and sparing me (and others) the hassle.

Quoted 6 lines
Quoted 6 lines
builtin-fast-export_new.c renamed from builtin-fast-export.c +0 -0
@@ -442,8 +442,9 @@ static void export_marks(char *file)
 		deco++;
 	}
 
Quoted 8 lines

builtin-fast-export_new.c static void export_marks(char *file) · line 447

Quoted 4 lines
builtin-fast-export_new.c renamed from builtin-fast-export.c +2 -1
-	if (ferror(f) || fclose(f))
+	if (ferror(f))
 		error("Unable to write marks file %s.", file);
+  	fclose(f);

You no longer check the error returned by fclose(). This is important, because the FILE* API may buffer writes, and a write error may only become apparent when fclose() flushes the file.

Indeed. A better fix would be to replace the || by a |, but this must be accompanied by a comment so it does not get removed due to overzealous compiler warnings.

Ciao,
Dscho

#3 lore raw
Quoted 12 lines · Johannes Schindelin
Quoted 9 lines · Thomas Rast

builtin-fast-export_new.c static void export_marks(char *file) · line 447

Quoted 4 lines · Martin Ettl
builtin-fast-export_new.c renamed from builtin-fast-export.c +2 -1
-	if (ferror(f) || fclose(f))
+	if (ferror(f))
 		error("Unable to write marks file %s.", file);
+  	fclose(f);

You no longer check the error returned by fclose(). This is important, because the FILE* API may buffer writes, and a write error may only become apparent when fclose() flushes the file.

Quoted 3 lines

Indeed. A better fix would be to replace the || by a |, but this must be accompanied by a comment so it does not get removed due to overzealous compiler warnings.

Are you allowed to do that? IIRC using | no longer guarantees that ferror() is called before fclose(), and my local 'man 3p fclose' says that

After the call to fclose(), any use of stream results in undefined behavior.

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#4 lore raw
Quoted 11 lines · Thomas Rast
Quoted 10 lines · Johannes Schindelin
Quoted 8 lines · Thomas Rast

builtin-fast-export_new.c static void export_marks(char *file) · line 447

Quoted 4 lines · Martin Ettl
builtin-fast-export_new.c renamed from builtin-fast-export.c +2 -1
-	if (ferror(f) || fclose(f))
+	if (ferror(f))
 		error("Unable to write marks file %s.", file);
+  	fclose(f);

You no longer check the error returned by fclose(). This is important, because the FILE* API may buffer writes, and a write error may only become apparent when fclose() flushes the file.

Quoted 9 lines
Quoted 1 line

Indeed. A better fix would be to replace the || by a |, but this must be accompanied by a comment so it does not get removed due to overzealous compiler warnings.

Are you allowed to do that? IIRC using | no longer guarantees that ferror() is called before fclose(), and my local 'man 3p fclose' says that

After the call to fclose(), any use of stream results in undefined behavior.

A more important question; Do we really care? I haven't looked closely at the code, but afair the marks file is written once per invocation, so leaking its file descriptor sounds like something we won't really bother about.

Signature · 7 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

Considering the successes of the wars on alcohol, poverty, drugs and
terror, I think we should give some serious thought to declaring war
on peace.
#5 lore raw
In reply to Thomas Rast

Hi,

Quoted 17 lines · Thomas Rast
Quoted 12 lines · Johannes Schindelin
Quoted 9 lines · Thomas Rast

builtin-fast-export_new.c static void export_marks(char *file) · line 447

Quoted 4 lines · Martin Ettl
builtin-fast-export_new.c renamed from builtin-fast-export.c +2 -1
-	if (ferror(f) || fclose(f))
+	if (ferror(f))
 		error("Unable to write marks file %s.", file);
+  	fclose(f);

You no longer check the error returned by fclose(). This is important, because the FILE* API may buffer writes, and a write error may only become apparent when fclose() flushes the file.

Quoted 3 lines

Indeed. A better fix would be to replace the || by a |, but this must be accompanied by a comment so it does not get removed due to overzealous compiler warnings.

Quoted 6 lines

Are you allowed to do that? IIRC using | no longer guarantees that ferror() is called before fclose(), and my local 'man 3p fclose' says that

After the call to fclose(), any use of stream results in undefined behavior.

Good point. So we really need something like

	err = ferror(f);
	err |= fclose(f); /* call fclose() even if there was an error */
	if (err)
		error...

Ciao,
Dscho

#6 lore raw

PATCH 1/1 Fix export_marks() error handling.

- Don't leak one FILE * on error per export_marks() call. Found with cppcheck and reported by Martin Ettl.

- Abort the potentially long for(;idnums.size;) loop on write errors.

- Add a trailing full-stop to error message when fopen() fails.

---
 builtin-fast-export.c |   15 ++++++++++++---
 1 files changed, 12 insertions(+), 3 deletions(-)
builtin-fast-export.c +12 -3
index 9a8a6fc..6c0956d 100644
@@ -428,21 +428,30 @@ static void export_marks(char *file)
 	uint32_t mark;
 	struct object_decoration *deco = idnums.hash;
 	FILE *f;
+	int e;
 
 	f = fopen(file, "w");
 	if (!f)
-		error("Unable to open marks file %s for writing", file);
+		error("Unable to open marks file %s for writing.", file);
 
 	for (i = 0; i < idnums.size; i++) {
 		if (deco->base && deco->base->type == 1) {
 			mark = ptr_to_mark(deco->decoration);
-			fprintf(f, ":%"PRIu32" %s\n", mark,
+			e = fprintf(f, ":%"PRIu32" %s\n", mark,
 				sha1_to_hex(deco->base->sha1));
+			if (e < 0) break;
 		}
 		deco++;
 	}
 
-	if (ferror(f) || fclose(f))
+	/* do not optimize the next two lines - they must both be executed in
+	 * this order. || might short-circuit the fclose(), and combining them
+	 * into one statement might reverse the order of execution.
+	 * Also, fflush() may not be sufficient - on some file systems, the
+	 * error is still delayed until the final [f]close().  */
+	e  = ferror(f);
+	e |= fclose(f);
+	if (e)
 		error("Unable to write marks file %s.", file);
 }
 
Signature · 1 line
1.6.3.3.385.g60647
#7 lore raw

Re: [PATCH] Fix export_marks() error handling.

builtin-fast-export.c static void export_marks(char *file) · line 454

Quoted 8 lines · Matthias Andree
builtin-fast-export.c +8 -0
+	/* do not optimize the next two lines - they must both be executed in
+	 * this order. || might short-circuit the fclose(), and combining them
+	 * into one statement might reverse the order of execution.
+	 * Also, fflush() may not be sufficient - on some file systems, the
+	 * error is still delayed until the final [f]close().  */
+	e  = ferror(f);
+	e |= fclose(f);
+	if (e)

The commentary above should be common knowledge for anyone familiar with ANSI C. So I'd suggest moving the comments into the description section of the commit and removing them from the actual code.

Signature · 4 lines
Sincerely,
           Stephen R. van den Berg.

"And now for something *completely* different!"
#9 lore raw
In reply to Johannes Schindelin

Am 09.07.2009, 15:01 Uhr, schrieb Johannes Schindelin <[email protected]>:

Quoted 30 lines

Hi,

Quoted 19 lines · Thomas Rast
Quoted 10 lines · Johannes Schindelin
Quoted 8 lines · Thomas Rast

builtin-fast-export_new.c static void export_marks(char *file) · line 447

Quoted 4 lines · Martin Ettl
builtin-fast-export_new.c renamed from builtin-fast-export.c +2 -1
-	if (ferror(f) || fclose(f))
+	if (ferror(f))
 		error("Unable to write marks file %s.", file);
+  	fclose(f);

You no longer check the error returned by fclose(). This is important, because the FILE* API may buffer writes, and a write

error

Quoted 3 lines
Quoted 1 line

may only become apparent when fclose() flushes the file.

Indeed. A better fix would be to replace the || by a |, but this

must be

Quoted 2 lines

accompanied by a comment so it does not get removed due to overzealous compiler warnings.

Quoted 6 lines

Are you allowed to do that? IIRC using | no longer guarantees that ferror() is called before fclose(), and my local 'man 3p fclose' says that

After the call to fclose(), any use of stream results in undefined behavior.

Quoted 6 lines

Good point. So we really need something like

	err = ferror(f);
	err |= fclose(f); /* call fclose() even if there was an error */
	if (err)
		error...

I've made such a patch, to appear soon on the list (sorry for not Cc:'ing it).

Signature · 1 line
Matthias Andree
#8 lore raw

Re: [PATCH] Fix export_marks() error handling.

In reply to Stephen R. van den Berg

Am 11.07.2009, 11:45 Uhr, schrieb Stephen R. van den Berg <[email protected]>:

Quoted 13 lines

builtin-fast-export.c static void export_marks(char *file) · line 454

Quoted 8 lines · Matthias Andree
builtin-fast-export.c +8 -0
+	/* do not optimize the next two lines - they must both be executed in
+	 * this order. || might short-circuit the fclose(), and combining them
+	 * into one statement might reverse the order of execution.
+	 * Also, fflush() may not be sufficient - on some file systems, the
+	 * error is still delayed until the final [f]close().  */
+	e  = ferror(f);
+	e |= fclose(f);
+	if (e)

The commentary above should be common knowledge for anyone familiar with ANSI C. So I'd suggest moving the comments into the description section of the commit and removing them from the actual code.

Feel free to do it and submit a patch, I'm not going to invest more time into a piece of code that runs seldomly.

Signature · 1 line
Matthias Andree