Skip to content

Commit 5781a9a

Browse files
novalisgitster
authored andcommitted
xgethostname: handle long hostnames
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent da25bdb commit 5781a9a

File tree

6 files changed

+19
-4
lines changed

6 files changed

+19
-4
lines changed

builtin/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
232232
/* already locked */
233233
return NULL;
234234

235-
if (gethostname(my_host, sizeof(my_host)))
235+
if (xgethostname(my_host, sizeof(my_host)))
236236
xsnprintf(my_host, sizeof(my_host), "unknown");
237237

238238
pidfile_path = git_pathdup("gc.pid");

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,7 @@ static const char *unpack(int err_fd, struct shallow_info *si)
16601660
argv_array_pushl(&child.args, "index-pack",
16611661
"--stdin", hdr_arg, NULL);
16621662

1663-
if (gethostname(hostname, sizeof(hostname)))
1663+
if (xgethostname(hostname, sizeof(hostname)))
16641664
xsnprintf(hostname, sizeof(hostname), "localhost");
16651665
argv_array_pushf(&child.args,
16661666
"--keep=receive-pack %"PRIuMAX" on %s",

fetch-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ static int get_pack(struct fetch_pack_args *args,
746746
argv_array_push(&cmd.args, "--fix-thin");
747747
if (args->lock_pack || unpack_limit) {
748748
char hostname[HOST_NAME_MAX + 1];
749-
if (gethostname(hostname, sizeof(hostname)))
749+
if (xgethostname(hostname, sizeof(hostname)))
750750
xsnprintf(hostname, sizeof(hostname), "localhost");
751751
argv_array_pushf(&cmd.args,
752752
"--keep=fetch-pack %"PRIuMAX " on %s",

git-compat-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,8 @@ extern int xsnprintf(char *dst, size_t max, const char *fmt, ...);
882882
#define HOST_NAME_MAX 256
883883
#endif
884884

885+
extern int xgethostname(char *buf, size_t len);
886+
885887
/* in ctype.c, for kwset users */
886888
extern const unsigned char tolower_trans_tbl[256];
887889

ident.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static void add_domainname(struct strbuf *out, int *is_bogus)
122122
{
123123
char buf[HOST_NAME_MAX + 1];
124124

125-
if (gethostname(buf, sizeof(buf))) {
125+
if (xgethostname(buf, sizeof(buf))) {
126126
warning_errno("cannot get host name");
127127
strbuf_addstr(out, "(none)");
128128
*is_bogus = 1;

wrapper.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,3 +679,16 @@ void sleep_millisec(int millisec)
679679
{
680680
poll(NULL, 0, millisec);
681681
}
682+
683+
int xgethostname(char *buf, size_t len)
684+
{
685+
/*
686+
* If the full hostname doesn't fit in buf, POSIX does not
687+
* specify whether the buffer will be null-terminated, so to
688+
* be safe, do it ourselves.
689+
*/
690+
int ret = gethostname(buf, len);
691+
if (!ret)
692+
buf[len - 1] = 0;
693+
return ret;
694+
}

0 commit comments

Comments
 (0)