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

Re: [PATCH/RFC 02/11] strbuf: add non-variadic function strbuf_vaddf()

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 26, 2009, 18:46 UTC
Message-ID
<7vbpip86q5.fsf@alter.siamese.dyndns.org>
In-Reply-To
<40aa078e0911260238rd0c90cag126709d1de5f50de@mail.gmail.com>
Erik Faye-Lund <kusmabite@googlemail.com> writes:
Show 33 quoted lines
> On Thu, Nov 26, 2009 at 1:59 AM, Junio C Hamano <gitster@pobox.com> wrote:
>> Erik Faye-Lund <kusmabite@googlemail.com> writes:
>>
>>> +void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
>>>  {
>>>       int len;
>>>
>>>       if (!strbuf_avail(sb))
>>>               strbuf_grow(sb, 64);
>>>       len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
>>>       if (len < 0)
>>>               die("your vsnprintf is broken");
>>>       if (len > strbuf_avail(sb)) {
>>>               strbuf_grow(sb, len);
>>>               len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
>>>               if (len > strbuf_avail(sb)) {
>>>                       die("this should not happen, your snprintf is broken");
>>>               }
>>
>> Hmm, I would have expected to see va_copy() somewhere in the patch text.
>> Is it safe to reuse ap like this in two separate invocations of
>> vsnprintf()?
>
> I think your expectation is well justified, this seems to be a
> portability-bug waiting to happen. Sorry for missing this prior to
> sending out - on Windows this is known to work, and this function is
> currently only used from the Windows implementation of syslog.
>
> How kosher is it to use va_copy in the git-core, considering that it's
> C99? A quick grep reveals only one occurrence of va_copy in the
> source, and that's in compat/winansi.c. Searching the history of next
> reveals that Alex Riesen (CC'd) already removed one occurrence
> (4bf5383), so I'm starting to get slightly scared it might not be OK.

We tend to avoid C99 features and it saved us in a few occasions. Recent MSVC port revealed that we still had a handful of decl-after-statments but luckily the necessary fix-ups were minimal because I have been reasonably careful to reject patches that add it long before MSVC port happened.

Show 6 quoted lines
> In practice it seems that something like the following works
> portably-enough for many applications, dunno if it's something we'll
> be happy with:
> #ifndef va_copy
> #define va_copy(a,b) ((a) = (b))
> #endif

Since an obvious implementation of va_list would be to make it a pointer into the stack frame, doing the above would work on many systems. On esoteric systems that needs something different (e.g. where va_list is implemented as a size-1 array of pointers, va_copy(a,b) needs to be an assignment (*(a) = *(b))), people can add compatibility macro later.

Historically some systems that do have a suitable implementation had it under the name __va_copy() instead, so it would have been better to define it as something like:

    #ifndef va_copy
    # ifdef __va_copy
    # define va_copy(a,b) __va_copy(a,b)
    # else
    # /* fallback for the most obvious implementation of va_list */
    # define va_copy(a,b) ((a) = (b))
    # endif
    #endif
But I do not know it still matters in practice anymore.
Previous: Paolo BonziniNext: Erik Faye-Lund
Message 42 of 54 in “daemon-win32”
  1. 00/11 daemon-win32Erik Faye-Lund, Nov 26, 2009
  2. 01/11 mingw: add network-wrappers for daemonErik Faye-Lund, Nov 26, 2009
  3. 02/11 strbuf: add non-variadic function strbuf_vaddf()Erik Faye-Lund, Nov 26, 2009
  4. 03/11 mingw: implement syslogErik Faye-Lund, Nov 26, 2009
  5. 04/11 compat: add inet_pton and inet_ntop prototypesErik Faye-Lund, Nov 26, 2009
  6. 05/11 inet_ntop: fix a couple of old-style declsErik Faye-Lund, Nov 26, 2009
  7. 06/11 run-command: add kill_async() and is_async_alive()Erik Faye-Lund, Nov 26, 2009
  8. 07/11 run-command: support input-fdErik Faye-Lund, Nov 26, 2009
  9. 08/11 daemon: use explicit file descriptorErik Faye-Lund, Nov 26, 2009
  10. 09/11 daemon: use run-command api for async servingErik Faye-Lund, Nov 26, 2009
  11. 10/11 daemon: use full buffered mode for stderrErik Faye-Lund, Nov 26, 2009
  12. 11/11 mingw: compile git-daemonErik Faye-Lund, Nov 26, 2009
  13. Johannes SixtNov 27, 2009
  14. Johannes SixtNov 27, 2009
  15. Erik Faye-LundDec 2, 2009
  16. Johannes SixtDec 2, 2009
  17. Erik Faye-LundDec 8, 2009
  18. Johannes SixtNov 26, 2009
  19. Erik Faye-LundNov 27, 2009
  20. Erik Faye-LundNov 27, 2009
  21. Johannes SixtNov 27, 2009
  22. Johannes SixtNov 27, 2009
  23. Erik Faye-LundDec 8, 2009
  24. Johannes SixtNov 26, 2009
  25. Erik Faye-LundNov 27, 2009
  26. Johannes SixtNov 27, 2009
  27. Erik Faye-LundDec 8, 2009
  28. Johannes SixtNov 26, 2009
  29. Erik Faye-LundNov 27, 2009
  30. Johannes SixtNov 27, 2009
  31. Erik Faye-LundDec 2, 2009
  32. Johannes SixtDec 2, 2009
  33. Erik Faye-LundJan 9, 2010
  34. Erik Faye-LundJan 10, 2010
  35. Johannes SixtNov 26, 2009
  36. Erik Faye-LundNov 27, 2009
  37. Johannes SixtNov 27, 2009
  38. Erik Faye-LundDec 8, 2009
  39. Junio C HamanoNov 26, 2009
  40. Erik Faye-LundNov 26, 2009
  41. Paolo BonziniNov 26, 2009
  42. Junio C HamanoNov 26, 2009
  43. Erik Faye-LundNov 26, 2009
  44. Johannes SixtNov 27, 2009
  45. Martin StorsjöNov 26, 2009
  46. Improve the mingw getaddrinfo stub to handle more use casesMartin Storsjö, Nov 26, 2009
  47. Erik Faye-LundNov 26, 2009
  48. Martin StorsjöNov 26, 2009
  49. Erik Faye-LundDec 2, 2009
  50. Martin StorsjöDec 2, 2009
  51. Erik Faye-LundDec 2, 2009
  52. Erik Faye-LundDec 2, 2009
  53. Johannes SixtDec 2, 2009
  54. Johannes SixtNov 26, 2009

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.