Subject: Re: lib/34744 (Missing documentation of struct sockaddr member "sun_len" in unix(4))
To: None <lib-bug-people@netbsd.org, gnats-admin@netbsd.org,>
From: Christian Biere <christianbiere@gmx.de>
List: netbsd-bugs
Date: 10/08/2006 19:15:06
The following reply was made to PR lib/34744; it has been noted by GNATS.

From: Christian Biere <christianbiere@gmx.de>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: lib/34744 (Missing documentation of struct sockaddr member "sun_len" in unix(4))
Date: Sun, 8 Oct 2006 21:11:53 +0200

 pooka@netbsd.org wrote:
 > Synopsis: Missing documentation of struct sockaddr member "sun_len" in unix(4)
 > 
 > State-Changed-From-To: open->feedback
 > State-Changed-By: pooka@netbsd.org
 > State-Changed-When: Sun, 08 Oct 2006 16:09:40 +0300
 > State-Changed-Why:
 > example added, ok?
 
 You explain the SUN_LEN() macro but the struct member "sun_len" is still
 unexplained. If it can be ignored or should be set to 0, the manpage
 should explicitely state this.
 
 Also I'm not quite happy with the example. Either drop anything but
 the bind() line or rather write it like this:
 
 	sockaddr_un addr;
 	int ret;
 
 	if (strlen(path) >= sizeof addr.sun_path)
 		goto path_is_too_long;
 
 	memset(&addr, 0, sizeof addr);
 	addr.sun_family = AF_LOCAL;
 	strncpy(addr->sun_path, path, sizeof addr.sun_path);
 	ret = bind(s, (const struct sockaddr *) &addr, SUN_LEN(&addr));
 	if (-1 == ret)
 		goto bind_failed;
 
 But that's getting bloated. Honestly, I just cared about the line
 which shows where to put SUN_LEN(). Using strlcpy() without checking
 its return value is usually a bug and should not be encouraged.
 Especially here where sizeof addr.sun_path is comparatively tiny
 (about 108), the path might easily get truncated.
 
 As your provided example shows, AF_LOCAL and/or AF_UNIX should
 indeed be mentioned here as well.
 
 SUN_LEN() might actually be completely redundant. At least according
 to all information I find, sizeof addr will work everywhere. Infact,
 the macro below is not portable e.g., it truncates the path on IRIX
 with connect():
 
 #define SUN_LEN(su) \
 	(sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
 
 So for portability reasons, use of this macro should maybe be
 discouraged. Last but not least,
 
 	$ grep -rli sun_len /usr/src
 
 shows quite inconsistent use of sun_len, SUN_LEN() and the
 size parameter passed to bind() and connect().
 
 -- 
 Christian