]> git.saurik.com Git - apple/libc.git/blame_incremental - gen/FreeBSD/gethostname.c.patch
Libc-498.tar.gz
[apple/libc.git] / gen / FreeBSD / gethostname.c.patch
... / ...
CommitLineData
1--- gethostname.c.orig 2004-11-25 11:38:01.000000000 -0800
2+++ gethostname.c 2005-09-15 09:46:13.000000000 -0700
3@@ -39,6 +39,7 @@ __FBSDID("$FreeBSD: src/lib/libc/gen/get
4
5 #include <sys/param.h>
6 #include <sys/sysctl.h>
7+#include <limits.h>
8
9 #include <errno.h>
10
11@@ -54,10 +55,22 @@ gethostname(name, namelen)
12
13 mib[0] = CTL_KERN;
14 mib[1] = KERN_HOSTNAME;
15+ if (namelen < MAXHOSTNAMELEN + 1) {
16+ char local_buf[MAXHOSTNAMELEN + 1];
17+ size_t local_len = sizeof(local_buf);
18+ if (sysctl(mib, 2, local_buf, &local_len, NULL, 0) == -1) {
19+ if (errno == ENOMEM)
20+ errno = ENAMETOOLONG;
21+ return (-1);
22+ }
23+ strncpy(name, local_buf, namelen);
24+ name[namelen - 1] = 0;
25+ } else {
26 if (sysctl(mib, 2, name, &namelen, NULL, 0) == -1) {
27 if (errno == ENOMEM)
28 errno = ENAMETOOLONG;
29 return (-1);
30+ }
31 }
32 return (0);
33 }