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

Re: [PATCH v3 2/2] t/helper/test-delta: fix possible resource leak and ensure safe cleanup

From
Jeff King <peff@peff.net>
Date
Jul 23, 2025, 07:55 UTC
Message-ID
<20250723075513.GA570540@coredump.intra.peff.net>
In-Reply-To
<CAPig+cR-r=CeEaSTeWsX00MLCSRJUUVXMUWS6Ui-HQcR_qMGJA@mail.gmail.com>
On Wed, Jul 23, 2025 at 03:28:05AM -0400, Eric Sunshine wrote:
Show 20 quoted lines
> >   close(fd);
> 
> The descriptor is closed manually (again) because a subsequent open()
> call is going to reuse the variable. However...
> 
> >   if (argv[1][1] == 'd')
> >     out_buf = diff_delta(from_buf, from_size,
> >              data_buf, data_size,
> >              &out_size, 0);
> >   else
> >     out_buf = patch_delta(from_buf, from_size,
> >               data_buf, data_size,
> >               &out_size);
> >   if (!out_buf) {
> >     fprintf(stderr, "delta operation failed (returned NULL)\n");
> >     goto cleanup;
> >   }
> 
> ...although `fd` was closed, it still holds the previously-open
> non-negative file descriptor, which means that this `goto cleanup`...

Oof, good catch. This iteration of the patch was based on my suggestion, but I didn't notice the jump to cleanup between that close/open pair.

I wonder if it would be more clear written as:
  int from_fd = -1;
  int data_fd = -1;
  int out_fd = -1;
  ...
  from_fd = open(...);
  if (...errors...)
	goto cleanup;
  close(from_fd);
  from_fd = -1;
  [...same for other fds...]
cleanup:
  if (from_fd >= 0)
	close(from_fd);
  [etc]

You could even drop that first close() in the happy path entirely, if you don't mind holding all three descriptors open at once.

I dunno. We are reaching diminishing returns spending brainpower on a function that is meant to be somewhat quick-and-dirty.

-Peff
Previous: Eric SunshineNext: Jeff King
Message 6 of 18 in “fix resource leaks in test helpers”
  1. 0/2 fix resource leaks in test helpersHoyoung Lee, Jul 22, 2025
  2. 1/2 t/helper/test-truncate: close file descriptor after truncationHoyoung Lee, Jul 22, 2025
  3. Junio C HamanoJul 22, 2025
  4. 2/2 t/helper/test-delta: fix possible resource leak and ensure safe cleanupHoyoung Lee, Jul 22, 2025
  5. Eric SunshineJul 23, 2025
  6. Jeff KingJul 23, 2025
  7. Jeff KingJul 23, 2025
  8. Eric SunshineJul 23, 2025
  9. Jeff KingJul 23, 2025
  10. 1/3 test-delta: handle errors with die()Jeff King, Jul 24, 2025
  11. 2/3 test-delta: use strbufs to hold input filesJeff King, Jul 24, 2025
  12. 3/3 test-delta: close output descriptor after useJeff King, Jul 24, 2025
  13. Eric SunshineJul 23, 2025
  14. Jeff KingJul 23, 2025
  15. Eric SunshineJul 23, 2025
  16. Junio C HamanoJul 23, 2025
  17. Junio C HamanoJul 23, 2025
  18. Junio C HamanoJul 23, 2025

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.