git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 02/13] loose: avoid closing invalid fd on error path

From
Patrick Steinhardt <ps@pks.im>
Date
Jul 1, 2026, 07:56 UTC
Message-ID
<akTIIaxwqqFaVxj1@pks.im>
In-Reply-To
<a1cd229e33c0ecf8ccbef9ab07b4b93896eae22e.1782889472.git.gitgitgadget@gmail.com>
On Wed, Jul 01, 2026 at 07:04:20AM +0000, Johannes Schindelin via GitGitGadget wrote:
Show 14 quoted lines
> diff --git a/loose.c b/loose.c
> index 47b7f5ec38..2c6db45245 100644
> --- a/loose.c
> +++ b/loose.c
> @@ -202,7 +202,8 @@ static int write_one_object(struct odb_source_loose *loose,
>  	return 0;
>  errout:
>  	error_errno(_("failed to write loose object index %s"), path.buf);
> -	close(fd);
> +	if (fd >= 0)
> +		close(fd);
>  	rollback_lock_file(&lock);
>  	strbuf_release(&buf);
>  	strbuf_release(&path);

Makes sense. At the time we hit the first `goto errout` we have already assigned `fd = open(...)`, so we know it should be either negative or a positive file descriptor.

There's also a second call to `close(fd)`, but if that call is successful then we would not use the `errout` path. If it fails we may try to close the file descriptor a second time, but that's probably a non-issue.

Patrick
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 7 of 44 in “coverity: fix leaks and error paths”
  1. 00/13 coverity: fix leaks and error pathsJohannes Schindelin via GitGitGadget, Jul 1, 2026
  2. 01/13 load_one_loose_object_map(): fix resource leakJohannes Schindelin via GitGitGadget, Jul 1, 2026
  3. Patrick SteinhardtJul 1, 2026
  4. Junio C HamanoJul 1, 2026
  5. Johannes SchindelinJul 4, 2026
  6. 02/13 loose: avoid closing invalid fd on error pathJohannes Schindelin via GitGitGadget, Jul 1, 2026
  7. Patrick SteinhardtJul 1, 2026
  8. 03/13 download_https_uri_to_file(): do not leak fd upon failureJohannes Schindelin via GitGitGadget, Jul 1, 2026
  9. 04/13 run-command: avoid close(-1) in start_command() error pathsJohannes Schindelin via GitGitGadget, Jul 1, 2026
  10. Patrick SteinhardtJul 1, 2026
  11. 05/13 run_diff_files: avoid memory leakJohannes Schindelin via GitGitGadget, Jul 1, 2026
  12. Patrick SteinhardtJul 1, 2026
  13. Johannes SchindelinJul 4, 2026
  14. 06/13 line-log: avoid redundant copy that leaks in process_rangesJohannes Schindelin via GitGitGadget, Jul 1, 2026
  15. Jeff KingJul 1, 2026
  16. 07/13 dir: free allocations on parse-error paths in read_one_dir()Johannes Schindelin via GitGitGadget, Jul 1, 2026
  17. Patrick SteinhardtJul 1, 2026
  18. Johannes SchindelinJul 4, 2026
  19. 08/13 submodule: fix cwd leak in get_superproject_working_tree()Johannes Schindelin via GitGitGadget, Jul 1, 2026
  20. Patrick SteinhardtJul 1, 2026
  21. Johannes SchindelinJul 4, 2026
  22. 09/13 worktree: fix resource leaks when branch creation failsJohannes Schindelin via GitGitGadget, Jul 1, 2026
  23. 10/13 imap-send: avoid leaking the IMAP upload bufferJohannes Schindelin via GitGitGadget, Jul 1, 2026
  24. 11/13 reftable/table: release filter on error pathJohannes Schindelin via GitGitGadget, Jul 1, 2026
  25. 12/13 fsmonitor: plug token-data leak on early daemon-startup failuresJohannes Schindelin via GitGitGadget, Jul 1, 2026
  26. 13/13 mingw: make exit_process() own the process handle on all pathsJohannes Schindelin via GitGitGadget, Jul 1, 2026
  27. Junio C HamanoJul 1, 2026
  28. 00/12 coverity: fix leaks and error pathsJohannes Schindelin via GitGitGadget, Jul 5, 2026
  29. 01/12 load_one_loose_object_map(): fix resource leakJohannes Schindelin via GitGitGadget, Jul 5, 2026
  30. Patrick SteinhardtJul 7, 2026
  31. 02/12 loose: avoid closing invalid fd on error pathJohannes Schindelin via GitGitGadget, Jul 5, 2026
  32. 03/12 download_https_uri_to_file(): do not leak fd upon failureJohannes Schindelin via GitGitGadget, Jul 5, 2026
  33. 04/12 run-command: avoid `close(-1)` in `start_command()` error pathsJohannes Schindelin via GitGitGadget, Jul 5, 2026
  34. 05/12 line-log: avoid redundant copy that leaks in process_rangesJohannes Schindelin via GitGitGadget, Jul 5, 2026
  35. 06/12 dir: free allocations on parse-error paths in `read_one_dir()`Johannes Schindelin via GitGitGadget, Jul 5, 2026
  36. 07/12 submodule: fix cwd leak in `get_superproject_working_tree()`Johannes Schindelin via GitGitGadget, Jul 5, 2026
  37. Patrick SteinhardtJul 7, 2026
  38. 08/12 worktree: fix resource leaks when branch creation failsJohannes Schindelin via GitGitGadget, Jul 5, 2026
  39. 09/12 imap-send: avoid leaking the IMAP upload bufferJohannes Schindelin via GitGitGadget, Jul 5, 2026
  40. 10/12 reftable/table: release filter on error pathJohannes Schindelin via GitGitGadget, Jul 5, 2026
  41. 11/12 fsmonitor: plug token-data leak on early daemon-startup failuresJohannes Schindelin via GitGitGadget, Jul 5, 2026
  42. 12/12 mingw: make `exit_process()` own the process handle on all pathsJohannes Schindelin via GitGitGadget, Jul 5, 2026
  43. Patrick SteinhardtJul 7, 2026
  44. Junio C HamanoJul 7, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.