1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-06 02:56:11 +02:00

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 <dturner@twosigma.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
David Turner 2017-04-18 17:57:43 -04:00 committed by Junio C Hamano
parent da25bdb776
commit 5781a9a270
6 changed files with 19 additions and 4 deletions

View 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");

View 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",

View 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",

View 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];

View File

@ -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;

View File

@ -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;
}