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
Junio C Hamano <gitster@pobox.com>
Date
Jul 23, 2025, 16:44 UTC
Message-ID
<xmqq5xfiq2ka.fsf@gitster.g>
In-Reply-To
<CAPig+cR-r=CeEaSTeWsX00MLCSRJUUVXMUWS6Ui-HQcR_qMGJA@mail.gmail.com>
Eric Sunshine <sunshine@sunshineco.com> writes:
Show 15 quoted lines
> On Tue, Jul 22, 2025 at 1:41 PM Hoyoung Lee <lhywkd22@gmail.com> wrote:
>> Initialize `fd` to -1 and unify all `open()`-related `close()` calls
>> under a single cleanup label. This prevents undefined behavior when
>> `fd` is used without initialization in error paths.
>
> It's not clear what this means. As far as I can tell, the original
> code never used an uninitialized `fd` in error paths.
>
>> The cleanup logic now safely avoids calling `close()` on invalid
>> descriptors and ensures consistent resource management.
>
> Again, it's not clear what this means. Although your previous version
> of this patch did add a call to close() with an invalid descriptor,
> the original code did not do so. So, the above statement seems to be
> misleading.

Great to see that good guidance is offered to new folks. Yes, it is a great thing to keep in mind to describe the latest iteration for readers who haven't even seen the previous iterations.

> Those issues aside, the patch itself has problems, some minor, such as
> making the code a bit confusing or misleading, and some major, such as
> calling close() on an already closed descriptor.
Show 26 quoted lines
>>   data_size = st.st_size;
>>   data_buf = xmalloc(data_size);
>>   if (read_in_full(fd, data_buf, data_size) < 0) {
>>     perror(argv[3]);
>>     goto cleanup;
>>   }
>>   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`...
Good eyes.
Show 17 quoted lines
>>   fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
>>   if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) {
>>     perror(argv[4]);
>>     goto cleanup;
>>   }
>>
>>   ret = 0;
>> cleanup:
>>   free(from_buf);
>>   free(data_buf);
>>   free(out_buf);
>>
>>   if (fd >= 0)
>>     close(fd);
>
> ...will arrive here and the condition will evaluate to "true",
> resulting in the already-closed descriptor being closed again.
Very clear description.
Thanks for your review.
Previous: Junio C Hamano
Message 18 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.