use HOST_NAME_MAX to size buffers for gethostname(2)

POSIX limits the length of host names to HOST_NAME_MAX. Export the fallback definition from daemon.c and use this constant to make all buffers used with gethostname(2) big enough for any possible result and a terminating NUL. Inspired-by: David Turner <dturner@twosigma.com> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: David Turner <dturner@twosigma.com> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Apr 18, 2017 at 17:57 UTC da25bdb7766c01665500cf7c7b75e76ea1f28b49
6 files changed +14 -10
builtin/gc.c
+7 -3
@@ -220,7 +220,7 @@ static int need_to_gc(void)
220 static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
221 {
222 static struct lock_file lock;
223 - char my_host[128];
223 + char my_host[HOST_NAME_MAX + 1];
224 struct strbuf sb = STRBUF_INIT;
225 struct stat st;
226 uintmax_t pid;
@@ -239,8 +239,12 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
239 fd = hold_lock_file_for_update(&lock, pidfile_path,
240 LOCK_DIE_ON_ERROR);
241 if (!force) {
242 - static char locking_host[128];
242 + static char locking_host[HOST_NAME_MAX + 1];
243 + static char *scan_fmt;
244 int should_exit;
245 +
246 + if (!scan_fmt)
247 + scan_fmt = xstrfmt("%s %%%dc", "%"SCNuMAX, HOST_NAME_MAX);
248 fp = fopen(pidfile_path, "r");
249 memset(locking_host, 0, sizeof(locking_host));
250 should_exit =
@@ -256,7 +260,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
260 * running.
261 */
262 time(NULL) - st.st_mtime <= 12 * 3600 &&
259 - fscanf(fp, "%"SCNuMAX" %127c", &pid, locking_host) == 2 &&
263 + fscanf(fp, scan_fmt, &pid, locking_host) == 2 &&
264 /* be gentle to concurrent "gc" on remote hosts */
265 (strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
266 if (fp != NULL)
builtin/receive-pack.c
+1 -1
@@ -1655,7 +1655,7 @@ static const char *unpack(int err_fd, struct shallow_info *si)
1655 if (status)
1656 return "unpack-objects abnormal exit";
1657 } else {
1658 - char hostname[256];
1658 + char hostname[HOST_NAME_MAX + 1];
1659
1660 argv_array_pushl(&child.args, "index-pack",
1661 "--stdin", hdr_arg, NULL);
daemon.c
-4
@@ -4,10 +4,6 @@
4 #include "strbuf.h"
5 #include "string-list.h"
6
7 -#ifndef HOST_NAME_MAX
8 -#define HOST_NAME_MAX 256
9 -#endif
10 -
7 #ifdef NO_INITGROUPS
8 #define initgroups(x, y) (0) /* nothing */
9 #endif
fetch-pack.c
+1 -1
@@ -745,7 +745,7 @@ static int get_pack(struct fetch_pack_args *args,
745 if (args->use_thin_pack)
746 argv_array_push(&cmd.args, "--fix-thin");
747 if (args->lock_pack || unpack_limit) {
748 - char hostname[256];
748 + char hostname[HOST_NAME_MAX + 1];
749 if (gethostname(hostname, sizeof(hostname)))
750 xsnprintf(hostname, sizeof(hostname), "localhost");
751 argv_array_pushf(&cmd.args,
git-compat-util.h
+4
@@ -878,6 +878,10 @@ static inline size_t xsize_t(off_t len)
878 __attribute__((format (printf, 3, 4)))
879 extern int xsnprintf(char *dst, size_t max, const char *fmt, ...);
880
881 +#ifndef HOST_NAME_MAX
882 +#define HOST_NAME_MAX 256
883 +#endif
884 +
885 /* in ctype.c, for kwset users */
886 extern const unsigned char tolower_trans_tbl[256];
887
ident.c
+1 -1
@@ -120,7 +120,7 @@ static int canonical_name(const char *host, struct strbuf *out)
120
121 static void add_domainname(struct strbuf *out, int *is_bogus)
122 {
123 - char buf[1024];
123 + char buf[HOST_NAME_MAX + 1];
124
125 if (gethostname(buf, sizeof(buf))) {
126 warning_errno("cannot get host name");