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

Re: [PATCH 3/3] sub-process: allocate argv on the heap

From
Stefan Beller <sbeller@google.com>
Date
Oct 3, 2017, 20:25 UTC
Message-ID
<CAGZ79kYPEeEh1ZG6M7bjMkA6V_XsU2LZCqdD0OxXU0h4p17FOQ@mail.gmail.com>
In-Reply-To
<20171003195713.13395-4-t.gummerer@gmail.com>
On Tue, Oct 3, 2017 at 12:57 PM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
Show 30 quoted lines
> Currently the argv is only allocated on the stack, and then assigned to
> process->argv.  When the start_subprocess function goes out of scope,
> the local argv variable is eliminated from the stack, but the pointer is
> still kept around in process->argv.
>
> Much later when we try to access the same process->argv in
> finish_command, this leads us to access a memory location that no longer
> contains what we want.  As argv0 is only used for printing errors, this
> is not easily noticed in normal git operations.  However when running
> t0021-conversion.sh through valgrind, valgrind rightfully complains:
>
> ==21024== Invalid read of size 8
> ==21024==    at 0x2ACF64: finish_command (run-command.c:869)
> ==21024==    by 0x2D6B18: subprocess_exit_handler (sub-process.c:72)
> ==21024==    by 0x2AB41E: cleanup_children (run-command.c:45)
> ==21024==    by 0x2AB526: cleanup_children_on_exit (run-command.c:81)
> ==21024==    by 0x54AD487: __run_exit_handlers (in /usr/lib/libc-2.26.so)
> ==21024==    by 0x54AD4D9: exit (in /usr/lib/libc-2.26.so)
> ==21024==    by 0x11A9EF: handle_builtin (git.c:550)
> ==21024==    by 0x11ABCC: run_argv (git.c:602)
> ==21024==    by 0x11AD8E: cmd_main (git.c:679)
> ==21024==    by 0x1BF125: main (common-main.c:43)
> ==21024==  Address 0x1ffeffec00 is on thread 1's stack
> ==21024==  1504 bytes below stack pointer
> ==21024==
>
> Fix this by allocating the memory on properly on the heap.  This memory
> is allocated on the heap, and never free'd.  However the same seems to be
> true for struct child_process, so it should be fine to just let the
> memory be free'd when the process terminates.
Uh. :( The broken window theory at work.

The patch below seems correct, but as you eluded to, now we'd be leaking memory. The run_command API has two fields 'char **argv' and 'argv_array args'. The argv is kept around for historical reasons as well as when the caller wants to be in control of the array (the caller needs to free the memory, but could also just reuse it for a slightly different invocation), whereas the args argument is owned by the child process, such that the memory is freed by finish_command.

As we're doing a memory allocation now anyway, how about:
-       const char *argv[] = { cmd, NULL };
...
    child_process_init(process);
+    argv_array_push(process.args, cmd);
Previous: Thomas GummererNext: Jeff King
Message 23 of 30 in “fixes for running the test suite with --valgrind”
  1. 0/3 fixes for running the test suite with --valgrindThomas Gummerer, Oct 3, 2017
  2. 1/3 path.c: fix uninitialized memory accessThomas Gummerer, Oct 3, 2017
  3. Jonathan NiederOct 3, 2017
  4. Jeff KingOct 3, 2017
  5. Jonathan NiederOct 3, 2017
  6. Junio C HamanoOct 4, 2017
  7. Jeff KingOct 4, 2017
  8. Thomas GummererOct 4, 2017
  9. Jonathan NiederOct 4, 2017
  10. 2/3 http-push: fix construction of hex value from pathThomas Gummerer, Oct 3, 2017
  11. Jonathan NiederOct 3, 2017
  12. Jeff KingOct 3, 2017
  13. Junio C HamanoOct 4, 2017
  14. Junio C HamanoOct 4, 2017
  15. Jeff KingOct 4, 2017
  16. Junio C HamanoOct 4, 2017
  17. 3/3 sub-process: allocate argv on the heapThomas Gummerer, Oct 3, 2017
  18. Johannes SixtOct 3, 2017
  19. Junio C HamanoOct 4, 2017
  20. Jeff KingOct 4, 2017
  21. Johannes SixtOct 4, 2017
  22. Thomas GummererOct 4, 2017
  23. Stefan BellerOct 3, 2017
  24. Jeff KingOct 3, 2017
  25. Jonathan NiederOct 3, 2017
  26. Jeff KingOct 3, 2017
  27. playing with MSan, was Re: [PATCH 0/3] fixes for running the test suite with --valgrindJeff King, Oct 4, 2017
  28. Thomas GummererOct 4, 2017
  29. lstat-ing delayed-filter output, was Re: playing with MSanJeff King, Oct 5, 2017
  30. Lars SchneiderOct 5, 2017

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.