Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Sep 2015 16:50:50 +0000 (UTC)
From:      Hiroki Sato <hrs@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r287404 - head/lib/libc/net
Message-ID:  <201509021650.t82GooQb015511@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: hrs
Date: Wed Sep  2 16:50:49 2015
New Revision: 287404
URL: https://svnweb.freebsd.org/changeset/base/287404

Log:
  - snprintf() returns at most size-1 of the chars printed into
    the buffer.  (n == hostlen) also means the buffer length was
    too short.
  
  - Use sdl->sdl_data only when (sdl->sdl_nlen > 0 && sdl->sdl_alen == 0)
    to prevent redundant output.

Modified:
  head/lib/libc/net/getnameinfo.c

Modified: head/lib/libc/net/getnameinfo.c
==============================================================================
--- head/lib/libc/net/getnameinfo.c	Wed Sep  2 16:48:03 2015	(r287403)
+++ head/lib/libc/net/getnameinfo.c	Wed Sep  2 16:50:49 2015	(r287404)
@@ -394,26 +394,22 @@ getnameinfo_link(const struct sockaddr *
 
 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
 		n = snprintf(host, hostlen, "link#%d", sdl->sdl_index);
-		if (n > hostlen) {
+		if (n >= hostlen) {
 			*host = '\0';
 			return (EAI_MEMORY);
 		}
 		return (0);
 	}
 
-	if (sdl->sdl_nlen > 0) {
-		if (sdl->sdl_nlen + 1 > hostlen) {
+	if (sdl->sdl_nlen > 0 && sdl->sdl_alen == 0) {
+		n = sdl->sdl_nlen;
+		if (n >= hostlen) {
 			*host = '\0';
 			return (EAI_MEMORY);
 		}
 		memcpy(host, sdl->sdl_data, sdl->sdl_nlen);
-		n = sdl->sdl_nlen;
-		host += n;
-		if (sdl->sdl_alen > 0) {
-			*host++ = ':';
-			n++;
-		}
-		hostlen -= n;
+		host[n] = '\0';
+		return (0);
 	}
 
 	switch (sdl->sdl_type) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201509021650.t82GooQb015511>