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

Re: [PATCH v4 3/3] upload-archive: use start_command instead of fork

From
Erik Faye-Lund <kusmabite@gmail.com>
Date
Nov 15, 2011, 17:44 UTC
Message-ID
<CABPQNSbu9bb5UG1ihi2vGX5ZkpvjfjaXrT8bhDGZHb=n9RbjSg@mail.gmail.com>
In-Reply-To
<20111115173715.GA4478@sigill.intra.peff.net>
On Tue, Nov 15, 2011 at 6:37 PM, Jeff King <peff@peff.net> wrote:
Show 34 quoted lines
> On Tue, Nov 15, 2011 at 01:11:46PM +0100, Thomas Rast wrote:
>
>> But after a closer look I think this patch just prodded it enough to
>> unearth long-existing undefined behaviour: prepare_argv() summarizes
>> to something like
>>
>> static void prepare_argv(const char **sent_argv, const char **argv)
>> {
>>       char *p, buf[4096];
>>
>>       for (p = buf;;) {
>>               len = packet_read_line(0, p, (buf + sizeof buf) - p);
>>               /* ... p always points into buf ... */
>>               sent_argv[sent_argc++] = p;
>>               p += len;
>>               *p++ = 0;
>>       }
>>       sent_argv[sent_argc] = NULL;
>> }
>>
>> The code appears to have looked like this ever since the addition of
>> that file back in 39345a2 (Add git-upload-archive, 2006-09-07).  So
>> the elements of sent_argv have apparently always pointed into the
>> stack-allocated 'buf'.
>
> Oh, yikes. That is definitely the problem, but it does come from
> c09cd77e. The prepare_argv function used to be "run_upload_archive", and
> it would prepare argv on the stack, call into write_archive with it, and
> then return; nobody else cares about the value afterwards.
>
> Erik's patch converts it into a function that writes the new argv into a
> parameter and returns, and the now-invalid stack-allocated memory is
> used by the calling function.
>
Outch. Thanks for spotting.
>> A quick band-aid would be to heap-allocate it instead:
>
> That works.  An even shorter band-aid is to mark it as "static".

Hmm, I seem to remember spotting it myself at some point and fixing it by marking it as static. I guess I must have forgot to push it...

> I think the code would be more readable if it just used the new
> argv_array.
>
Oooh, nice. The whole argv_array slipped past me, I like it!
Previous: Jeff KingNext: Jeff King
Message 10 of 17 in “port upload-archive to Windows”
  1. 0/3 port upload-archive to WindowsErik Faye-Lund, Oct 24, 2011
  2. 1/3 mingw: move poll out of sys-folderErik Faye-Lund, Oct 24, 2011
  3. 2/3 compat/win32/poll.c: upgrade from upstreamErik Faye-Lund, Oct 24, 2011
  4. 3/3 upload-archive: use start_command instead of forkErik Faye-Lund, Oct 24, 2011
  5. Jeff KingOct 24, 2011
  6. Thomas RastNov 15, 2011
  7. Jeff KingNov 15, 2011
  8. Thomas RastNov 15, 2011
  9. Jeff KingNov 15, 2011
  10. Erik Faye-LundNov 15, 2011
  11. Jeff KingNov 15, 2011
  12. Junio C HamanoNov 15, 2011
  13. Jeff KingNov 15, 2011
  14. 1/2 upload-archive: drop extra argument to prepare_argvJeff King, Nov 15, 2011
  15. upload-archive: use argv_array for sent parametersJeff King, Nov 15, 2011
  16. Jeff KingNov 15, 2011
  17. Andreas SchwabNov 15, 2011

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.