]> git.ipfire.org Git - thirdparty/git.git/commitdiff
xgethostname: handle long hostnames
authorDavid Turner <dturner@twosigma.com>
Tue, 18 Apr 2017 21:57:43 +0000 (17:57 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 19 Apr 2017 02:58:04 +0000 (19:58 -0700)
If the full hostname doesn't fit in the buffer supplied to
gethostname, POSIX does not specify whether the buffer will be
null-terminated, so to be safe, we should do it ourselves.  Introduce
new function, xgethostname, which ensures that there is always a \0
at the end of the buffer.

Signed-off-by: David Turner <dturner@twosigma.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/gc.c
builtin/receive-pack.c
fetch-pack.c
git-compat-util.h
ident.c
wrapper.c

index 5befd518fd1616c606efef9e4be15d6283811530..4f85610d87697e67368a57baa5550108fdeb6d10 100644 (file)
@@ -232,7 +232,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
                /* already locked */
                return NULL;
 
-       if (gethostname(my_host, sizeof(my_host)))
+       if (xgethostname(my_host, sizeof(my_host)))
                xsnprintf(my_host, sizeof(my_host), "unknown");
 
        pidfile_path = git_pathdup("gc.pid");
index 3ca8ebe0e10a559a8036e5705df684b110e3b641..eee5faaa244ec0252305adbeddf7d5c5fa2d4eb0 100644 (file)
@@ -1660,7 +1660,7 @@ static const char *unpack(int err_fd, struct shallow_info *si)
                argv_array_pushl(&child.args, "index-pack",
                                 "--stdin", hdr_arg, NULL);
 
-               if (gethostname(hostname, sizeof(hostname)))
+               if (xgethostname(hostname, sizeof(hostname)))
                        xsnprintf(hostname, sizeof(hostname), "localhost");
                argv_array_pushf(&child.args,
                                 "--keep=receive-pack %"PRIuMAX" on %s",
index a75ed6c940406cfe4befc6dbc61845f6af12cfc1..f43adfe7b55c3883d14449e7e577b7c65fac1320 100644 (file)
@@ -746,7 +746,7 @@ static int get_pack(struct fetch_pack_args *args,
                        argv_array_push(&cmd.args, "--fix-thin");
                if (args->lock_pack || unpack_limit) {
                        char hostname[HOST_NAME_MAX + 1];
-                       if (gethostname(hostname, sizeof(hostname)))
+                       if (xgethostname(hostname, sizeof(hostname)))
                                xsnprintf(hostname, sizeof(hostname), "localhost");
                        argv_array_pushf(&cmd.args,
                                        "--keep=fetch-pack %"PRIuMAX " on %s",
index 1f1e82407e1e93dbe7e47b94ca4aef01d4910d08..b8c4c8d638dfc9cdb261db3e64ab3eb33f3b5558 100644 (file)
@@ -882,6 +882,8 @@ extern int xsnprintf(char *dst, size_t max, const char *fmt, ...);
 #define HOST_NAME_MAX 256
 #endif
 
+extern int xgethostname(char *buf, size_t len);
+
 /* in ctype.c, for kwset users */
 extern const unsigned char tolower_trans_tbl[256];
 
diff --git a/ident.c b/ident.c
index 8ec0f25987540ccf64230b6aad4c6e2323d43455..7e386a48fa8331e69fae2a7b6a86b770e370cec5 100644 (file)
--- a/ident.c
+++ b/ident.c
@@ -122,7 +122,7 @@ static void add_domainname(struct strbuf *out, int *is_bogus)
 {
        char buf[HOST_NAME_MAX + 1];
 
-       if (gethostname(buf, sizeof(buf))) {
+       if (xgethostname(buf, sizeof(buf))) {
                warning_errno("cannot get host name");
                strbuf_addstr(out, "(none)");
                *is_bogus = 1;
index e7f197996868a614c84537ad96fc672ea901148d..64ebd6c690d70203578c0d928a1f1886ad2a6b0e 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -679,3 +679,16 @@ void sleep_millisec(int millisec)
 {
        poll(NULL, 0, millisec);
 }
+
+int xgethostname(char *buf, size_t len)
+{
+       /*
+        * If the full hostname doesn't fit in buf, POSIX does not
+        * specify whether the buffer will be null-terminated, so to
+        * be safe, do it ourselves.
+        */
+       int ret = gethostname(buf, len);
+       if (!ret)
+               buf[len - 1] = 0;
+       return ret;
+}