Skip to content

Commit f5ff5fb

Browse files
stefanbellergitster
authored andcommitted
bundle: don't leak an fd in case of early return
In successful operation `write_pack_data` will close the `bundle_fd`, but when we exit early, we need to take care of the file descriptor as well as the lock file ourselves. The lock file may be deleted at the end of running the program, but we are in library code, so we should not rely on that. Helped-by: Jeff King <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6eb6078 commit f5ff5fb

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

bundle.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,30 +435,41 @@ int create_bundle(struct bundle_header *header, const char *path,
435435

436436
/* write prerequisites */
437437
if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
438-
return -1;
438+
goto err;
439439

440440
argc = setup_revisions(argc, argv, &revs, NULL);
441441

442-
if (argc > 1)
443-
return error(_("unrecognized argument: %s"), argv[1]);
442+
if (argc > 1) {
443+
error(_("unrecognized argument: %s"), argv[1]);
444+
goto err;
445+
}
444446

445447
object_array_remove_duplicates(&revs.pending);
446448

447449
ref_count = write_bundle_refs(bundle_fd, &revs);
448450
if (!ref_count)
449451
die(_("Refusing to create empty bundle."));
450452
else if (ref_count < 0)
451-
return -1;
453+
goto err;
452454

453455
/* write pack */
454-
if (write_pack_data(bundle_fd, &revs))
455-
return -1;
456+
if (write_pack_data(bundle_fd, &revs)) {
457+
bundle_fd = -1; /* already closed by the above call */
458+
goto err;
459+
}
456460

457461
if (!bundle_to_stdout) {
458462
if (commit_lock_file(&lock))
459463
die_errno(_("cannot create '%s'"), path);
460464
}
461465
return 0;
466+
err:
467+
if (!bundle_to_stdout) {
468+
if (0 <= bundle_fd)
469+
close(bundle_fd);
470+
rollback_lock_file(&lock);
471+
}
472+
return -1;
462473
}
463474

464475
int unbundle(struct bundle_header *header, int bundle_fd, int flags)

0 commit comments

Comments
 (0)