]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/gethostname.c.patch
Libc-763.11.tar.gz
[apple/libc.git] / gen / FreeBSD / gethostname.c.patch
CommitLineData
1f2f436a
A
1--- gethostname.c.orig 2009-11-07 15:38:57.000000000 -0800
2+++ gethostname.c 2009-11-07 15:44:48.000000000 -0800
3@@ -33,8 +33,10 @@ static char sccsid[] = "@(#)gethostname.
34e8f829 4 #include <sys/cdefs.h>
1f2f436a 5 __FBSDID("$FreeBSD: src/lib/libc/gen/gethostname.c,v 1.8 2007/01/09 00:27:54 imp Exp $");
224c7076 6
34e8f829 7+#include <string.h>
224c7076
A
8 #include <sys/param.h>
9 #include <sys/sysctl.h>
10+#include <limits.h>
11
12 #include <errno.h>
1f2f436a
A
13 #include <unistd.h>
14@@ -48,10 +50,22 @@ gethostname(name, namelen)
224c7076
A
15
16 mib[0] = CTL_KERN;
17 mib[1] = KERN_HOSTNAME;
18+ if (namelen < MAXHOSTNAMELEN + 1) {
19+ char local_buf[MAXHOSTNAMELEN + 1];
20+ size_t local_len = sizeof(local_buf);
21+ if (sysctl(mib, 2, local_buf, &local_len, NULL, 0) == -1) {
22+ if (errno == ENOMEM)
23+ errno = ENAMETOOLONG;
24+ return (-1);
25+ }
26+ strncpy(name, local_buf, namelen);
27+ name[namelen - 1] = 0;
28+ } else {
29 if (sysctl(mib, 2, name, &namelen, NULL, 0) == -1) {
30 if (errno == ENOMEM)
31 errno = ENAMETOOLONG;
32 return (-1);
33+ }
34 }
35 return (0);
36 }