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

Re: [PATCH] compat/snprintf.c: handle snprintf's that always return the # chars transmitted

From
Johannes Sixt <j.sixt@viscovery.net>
Date
Aug 21, 2008, 08:06 UTC
Message-ID
<48AD2207.903@viscovery.net>
In-Reply-To
<nYV6zI97uDEOlIEFjH5oAZW1lTFm0mOVlTrIxzRyOY5CCrUJw7ROgA@cipher.nrlssc.navy.mil>
Brandon Casey schrieb:
Show 5 quoted lines
> Some platforms provide a horribly broken snprintf. More broken than the
> platforms that return -1 when there is too little space in the target buffer
> for the formatted string. Some platforms provide an snprintf which _always_
> returns the number of characters transmitted to the buffer, regardless of
> whether there was enough space or not.
...
Show 22 quoted lines
> diff --git a/compat/snprintf.c b/compat/snprintf.c
> index 580966e..357e733 100644
> --- a/compat/snprintf.c
> +++ b/compat/snprintf.c
> @@ -17,6 +17,8 @@ int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap)
>  
>  	if (maxsize > 0) {
>  		ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
> +		if (ret == maxsize-1)
> +			ret = -1;
>  		/* Windows does not NUL-terminate if result fills buffer */
>  		str[maxsize-1] = 0;
>  	}
> @@ -34,6 +36,8 @@ int git_vsnprintf(char *str, size_t maxsize, const char *format, va_list ap)
>  			break;
>  		s = str;
>  		ret = vsnprintf(str, maxsize-SNPRINTF_SIZE_CORR, format, ap);
> +		if (ret == maxsize-1)
> +			ret = -1;
>  	}
>  	free(s);
>  	return ret;
This looks good, and it passes the test suite on MinGW. Hence:
Acked-by: Johannes Sixt <johannes.sixt@telecom.at>
-- Hannes
Previous: Brandon Casey
Message 2 of 2 in “compat/snprintf.c: handle snprintf's that always return the # chars transmitted”
  1. compat/snprintf.c: handle snprintf's that always return the # chars transmittedBrandon Casey, Aug 21, 2008
  2. Johannes SixtAug 21, 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.