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. - 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matthias Andree committed
Jul 24, 2009 at 10:17 UTC
96d69b554325b6caa323428e64fba62ca033310d
1 file changed
+10
-4
builtin-fast-export.c
+10
-4
@@ -428,21 +428,27 @@ static void export_marks(char *file)
428
uint32_t mark;
429
struct object_decoration *deco = idnums.hash;
430
FILE *f;
431
+ int e = 0;
432
433
f = fopen(file, "w");
434
if (!f)
434
- error("Unable to open marks file %s for writing", file);
435
+ error("Unable to open marks file %s for writing.", file);
436
437
for (i = 0; i < idnums.size; i++) {
438
if (deco->base && deco->base->type == 1) {
439
mark = ptr_to_mark(deco->decoration);
439
- fprintf(f, ":%"PRIu32" %s\n", mark,
440
- sha1_to_hex(deco->base->sha1));
440
+ if (fprintf(f, ":%"PRIu32" %s\n", mark,
441
+ sha1_to_hex(deco->base->sha1)) < 0) {
442
+ e = 1;
443
+ break;
444
+ }
445
}
446
deco++;
447
}
448
445
- if (ferror(f) || fclose(f))
449
+ e |= ferror(f);
450
+ e |= fclose(f);
451
+ if (e)
452
error("Unable to write marks file %s.", file);
453
}
454