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

Re: [PATCH] Add compat/vsnprintf.c for systems that returns -1 on maxsize reached

From
Johannes Sixt <j.sixt@viscovery.net>
Date
Mar 5, 2008, 14:28 UTC
Message-ID
<47CEAE20.1030707@viscovery.net>
In-Reply-To
<200803051455.57148.michal.rokos@nextsoft.cz>
Michal Rokos schrieb:
Show 13 quoted lines
> On Wednesday 05 March 2008 10:18:10 Johannes Sixt wrote:
>> It's not the same on Windows, which returns:
>> case1: -1
>> case2: 5
>> case3: 5
>> case4: 5
>>
>> BTW, this is not only an issue of vsnprintf, but also of snprintf!
> 
> Hmm, HPUX has the same issue for snprint() as is for vsnprintf().
> 
> Do you think that following patch suffices your needs. Please note that it 
> actually copies data to str.

... in the case where the buffer is too small? This won't be a problem for our users.

Show 7 quoted lines
> +# Define SNPRINTF_RETURNS_BOGUS if your are on a system which snprintf()
> +# returns -1 instead of number of characters which would have been written
> +# to the final string if enough space had been available.
> +#
> +# Define VSNPRINTF_RETURNS_BOGUS if your are on a system which vsnprintf()
> +# returns -1 instead of number of characters which would have been written
> +# to the final string if enough space had been available.

We don't need two configuration variables. I think we can assume that if vsnprintf is broken, then snprintf will be broken, too:

# Define SNPRINTF_RETURNS_BOGUS if your are on a system which snprintf() # and vsnprintf() return -1 instead of number of characters that would # have been written to the final string if enough space had been # available.

Show 7 quoted lines
> +AC_CACHE_CHECK([whether snprintf() returns bogus],
> + [ac_cv_snprintf_returns_bogus],
> +[
> +AC_RUN_IFELSE(
> +	[AC_LANG_PROGRAM([AC_INCLUDES_DEFAULT],
> +		[[char buf[1];
> +		  if (snprintf(bug, 1, "%s", "12345") != 5) return 1]])],
                               ^^^
buf?

Are you trying to test for the second bogus behavior on Windows? Don't do it! I've thought about it for 5 minutes, but I can't come up with a simple test that would detect its odd behavior.

Show 16 quoted lines
> diff --git a/dev/null b/compat/snprintf.c
> new file mode 100644
> index 0000000..bc0d37c
> --- /dev/null
> +++ b/compat/snprintf.c
> @@ -0,0 +1,37 @@
> +#include "../git-compat-util.h"
> +
> +#undef vsnprintf
> +int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap)
> +{
> +   char *s;
> +   int size;
> +
> +   int ret = vsnprintf(str, maxsize, format, ap);
> +   if (ret != -1 ) return ret;

'return' goes on its own line. Indentation is one tabstop, not two spaces. Thank you.

> +
> +   s = NULL;
You could reuse str here.
> +   size = maxsize;

We are trying to find a suitably long buffer in a loop. We should spend as few cycles as possible. Therefore, my implementation used a minimum of 250*4 for the first try just in case the caller had a long string to construct. (And it protects against maxsize == 0.)

Show 5 quoted lines
> +   while ( ret == -1 )
> +   {
> +      size *= 4;
> +      s = realloc(s, size);
> +      if (! s) return -1;
Could you avoid the memory leak on this error path?
> +      ret = vsnprintf(s, size, format, ap);
> +   }
> +   if (str && maxsize > 0) memcpy(str, s, maxsize);
Why this?
> +   free(s);
> +   return ret;
> +}
-- Hannes
Previous: Michal RokosNext: Michal Rokos
Message 12 of 26 in “Add compat/vsnprintf.c for systems that returns -1 on maxsize reached”
  1. Add compat/vsnprintf.c for systems that returns -1 on maxsize reachedMichal Rokos, Mar 4, 2008
  2. Johannes SchindelinMar 4, 2008
  3. Finn Arne GangstadMar 4, 2008
  4. Johannes SixtMar 4, 2008
  5. Morten WelinderMar 4, 2008
  6. Johannes SixtMar 4, 2008
  7. Wayne DavisonMar 4, 2008
  8. Michal RokosMar 5, 2008
  9. Jeff KingMar 5, 2008
  10. Johannes SixtMar 5, 2008
  11. Michal RokosMar 5, 2008
  12. Johannes SixtMar 5, 2008
  13. Michal RokosMar 5, 2008
  14. Johannes SixtMar 5, 2008
  15. Michal RokosMar 5, 2008
  16. Wayne DavisonMar 5, 2008
  17. Johannes SixtMar 5, 2008
  18. Wayne DavisonMar 5, 2008
  19. Junio C HamanoMar 5, 2008
  20. Mike RalphsonMar 5, 2008
  21. Robert HainesMar 5, 2008
  22. Michal RokosMar 5, 2008
  23. Michal RokosMar 10, 2008
  24. Johannes SixtMar 10, 2008
  25. Michal RokosMar 10, 2008
  26. Johannes SixtMar 10, 2008

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.