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

Re: [PATCH v3 2/2] xgethostname: handle long hostnames

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 19, 2017, 02:51 UTC
Message-ID
<xmqq4lxlcdpf.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20170419013552.GB28740@aiede.svl.corp.google.com>
Jonathan Nieder <jrnieder@gmail.com> writes:
Show 36 quoted lines
> Hi,
>
> David Turner wrote:
>
>> If the full hostname doesn't fit in the buffer supplied to
>> gethostname, POSIX does not specify whether the buffer will be
>> null-terminated, so to be safe, we should do it ourselves.  Introduce
>> new function, xgethostname, which ensures that there is always a \0
>> at the end of the buffer.
>
> I think we should detect the error instead of truncating the hostname.
> That (on top of your patch) would look like the following.
>
> Thoughts?
> Jonathan
>
> diff --git i/wrapper.c w/wrapper.c
> index d837417709..e218bd3bef 100644
> --- i/wrapper.c
> +++ w/wrapper.c
> @@ -660,11 +660,13 @@ int xgethostname(char *buf, size_t len)
>  {
>  	/*
>  	 * If the full hostname doesn't fit in buf, POSIX does not
> -	 * specify whether the buffer will be null-terminated, so to
> -	 * be safe, do it ourselves.
> +	 * guarantee that an error will be returned. Check for ourselves
> +	 * to be safe.
>  	 */
>  	int ret = gethostname(buf, len);
> -	if (!ret)
> -		buf[len - 1] = 0;
> +	if (!ret && !memchr(buf, 0, len)) {
> +		errno = ENAMETOOLONG;
> +		return -1;
> +	}

Hmmmm. "Does not specify if the buffer will be NUL-terminated" would mean that it is OK for the platform gethostname() to stuff sizeof(buf)-1 first bytes of the hostname in the buffer and then truncate by placing '\0' at the end of the buf, and we would not notice truncation with the above change on such a platform, no?

Previous: Jonathan NiederNext: David Turner
Message 4 of 18 in “gethostbyname fixes”
  1. 0/2 gethostbyname fixesDavid Turner, Apr 18, 2017
  2. 2/2 xgethostname: handle long hostnamesDavid Turner, Apr 18, 2017
  3. Jonathan NiederApr 19, 2017
  4. Junio C HamanoApr 19, 2017
  5. David TurnerApr 19, 2017
  6. René ScharfeApr 19, 2017
  7. Junio C HamanoApr 19, 2017
  8. 1/2 use HOST_NAME_MAX to size buffers for gethostname(2)David Turner, Apr 18, 2017
  9. Jonathan NiederApr 19, 2017
  10. Junio C HamanoApr 19, 2017
  11. René ScharfeApr 19, 2017
  12. René ScharfeApr 19, 2017
  13. David TurnerApr 19, 2017
  14. Torsten BögershausenApr 19, 2017
  15. René ScharfeApr 19, 2017
  16. Torsten BögershausenApr 20, 2017
  17. René ScharfeApr 20, 2017
  18. Torsten BögershausenApr 21, 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.