Subject: lib/16634: [dM] nonportable examples in getaddrinfo(3)
To: None <gnats-bugs@gnats.netbsd.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: netbsd-bugs
Date: 05/03/2002 11:03:39
>Number: 16634
>Category: lib
>Synopsis: [dM] nonportable examples in getaddrinfo(3)
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: lib-bug-people
>State: open
>Class: doc-bug
>Submitter-Id: net
>Arrival-Date: Fri May 03 08:04:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: der Mouse
>Release: -current as of 2002-05-03
>Organization:
Dis-
>Environment:
Noticed on 1.4T, verified present in -current
>Description:
The sample code in the getaddrinfo(3) manpage uses memset(,0,)
to clear a struct addrinfo, even though three of the relevant
structure members are pointers. Even if this works on NetBSD,
it is bad code; nonportable assumptions like this have (or
certainly should have) no place in example code provided with
the system. I checked -current,
.\" $NetBSD: getaddrinfo.3,v 1.24 2002/02/07 09:24:06 ross Exp $
.\" $KAME: getaddrinfo.3,v 1.29 2001/02/12 09:24:45 itojun Exp $
and verified that the examples there are similarly defective.
>How-To-Repeat:
Read the EXAMPLES section of getaddrinfo(3). Note that it uses
memset to clear a structure that's documented as containing
pointers.
>Fix:
Replace the memset()s with a series of assignment statements.
This patch is for 1.4T's getaddrinfo.3, which has no $NetBSD$
line; it may need tweaking for -current (if nothing else, the
line numbers are probably off).
--- OLD/lib/libc/net/getaddrinfo.3 Thu Jan 1 00:00:00 1970
+++ NEW/lib/libc/net/getaddrinfo.3 Thu Jan 1 00:00:00 1970
@@ -357,9 +357,14 @@
int s;
const char *cause = NULL;
-memset(&hints, 0, sizeof(hints));
+hints.ai_flags = 0;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
+hints.ai_protocol = 0;
+hints.ai_addrlen = 0;
+hints.ai_canonname = 0;
+hints.ai_addr = 0;
+hints.ai_next = 0;
error = getaddrinfo("www.kame.net", "http", &hints, &res0);
if (error) {
err1(1, "%s", gai_strerror(error));
@@ -400,10 +405,13 @@
int nsock;
const char *cause = NULL;
-memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
+hints.ai_addrlen = 0;
+hints.ai_canonname = 0;
+hints.ai_addr = 0;
+hints.ai_next = 0;
error = getaddrinfo(NULL, "http", &hints, &res0);
if (error) {
err1(1, "%s", gai_strerror(error));
/~\ The ASCII der Mouse
\ / Ribbon Campaign
X Against HTML mouse@rodents.montreal.qc.ca
/ \ Email! 7D C8 61 52 5D E7 2D 39 4E F1 31 3E E8 B3 27 4B
>Release-Note:
>Audit-Trail:
>Unformatted: