# [PATCH] Fix export_marks() error handling.

2 messages from 2009-07-24 to 2009-07-24. Participants: Matthias Andree, Junio C Hamano.
Thread: https://gitlist.dev/t/20214

## Matthias Andree, 2009-07-24 08:17

Subject: [PATCH] Fix export_marks() error handling.
Message-ID: <1248423433-25407-1-git-send-email-matthias.andree@gmx.de>
URL: https://gitlist.dev/e/1248423433-25407-1-git-send-email-matthias.andree%40gmx.de
In-Reply-To: <7v7hxyyfcg.fsf@alter.siamese.dyndns.org>

```
- 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.

- Record error if fprintf() fails for reasons not required to set the
  stream error indicator, such as ENOMEM.

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

Signed-off-by: Matthias Andree <matthias.andree@gmx.de>
---
 builtin-fast-export.c |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/builtin-fast-export.c b/builtin-fast-export.c
index 9a8a6fc..ca19825 100644
--- a/builtin-fast-export.c
+++ b/builtin-fast-export.c
@@ -428,21 +428,27 @@ static void export_marks(char *file)
 	uint32_t mark;
 	struct object_decoration *deco = idnums.hash;
 	FILE *f;
+	int e = 0;
 
 	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,
-				sha1_to_hex(deco->base->sha1));
+			if (fprintf(f, ":%"PRIu32" %s\n", mark,
+				sha1_to_hex(deco->base->sha1)) < 0) {
+			    e = 1;
+			    break;
+			}
 		}
 		deco++;
 	}
 
-	if (ferror(f) || fclose(f))
+	e |= ferror(f);
+	e |= fclose(f);
+	if (e)
 		error("Unable to write marks file %s.", file);
 }
 
-- 
1.6.3.3.413.g91e7

```

## Junio C Hamano, 2009-07-24 16:35

Subject: Re: [PATCH] Fix export_marks() error handling.
Message-ID: <7vskgmkpi7.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vskgmkpi7.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1248423433-25407-1-git-send-email-matthias.andree@gmx.de>

```
Thanks, will apply.

```
