Skip to content

Commit 17ae0b8

Browse files
committed
Merge branch 'jk/sparse-fdleak-fix'
A file descriptor left open is now properly closed when "git sparse-checkout" updates the sparse patterns. * jk/sparse-fdleak-fix: sparse-checkout: use fdopen_lock_file() instead of xfdopen() sparse-checkout: check commit_lock_file when writing patterns sparse-checkout: consolidate cleanup when writing patterns
2 parents 0299251 + a71c478 commit 17ae0b8

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

builtin/sparse-checkout.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ static int write_patterns_and_update(struct pattern_list *pl)
327327
{
328328
char *sparse_filename;
329329
FILE *fp;
330-
int fd;
331330
struct lock_file lk = LOCK_INIT;
332331
int result;
333332

@@ -336,31 +335,31 @@ static int write_patterns_and_update(struct pattern_list *pl)
336335
if (safe_create_leading_directories(sparse_filename))
337336
die(_("failed to create directory for sparse-checkout file"));
338337

339-
fd = hold_lock_file_for_update(&lk, sparse_filename,
340-
LOCK_DIE_ON_ERROR);
341-
free(sparse_filename);
338+
hold_lock_file_for_update(&lk, sparse_filename, LOCK_DIE_ON_ERROR);
342339

343340
result = update_working_directory(pl);
344341
if (result) {
345342
rollback_lock_file(&lk);
346-
clear_pattern_list(pl);
347343
update_working_directory(NULL);
348-
return result;
344+
goto out;
349345
}
350346

351-
fp = xfdopen(fd, "w");
347+
fp = fdopen_lock_file(&lk, "w");
348+
if (!fp)
349+
die_errno(_("unable to fdopen %s"), get_lock_file_path(&lk));
352350

353351
if (core_sparse_checkout_cone)
354352
write_cone_to_file(fp, pl);
355353
else
356354
write_patterns_to_file(fp, pl);
357355

358-
fflush(fp);
359-
commit_lock_file(&lk);
356+
if (commit_lock_file(&lk))
357+
die_errno(_("unable to write %s"), sparse_filename);
360358

359+
out:
361360
clear_pattern_list(pl);
362-
363-
return 0;
361+
free(sparse_filename);
362+
return result;
364363
}
365364

366365
enum sparse_checkout_mode {

0 commit comments

Comments
 (0)