Skip to content

Commit da25bdb

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: David Turner <[email protected]> Reviewed-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c3808ca commit da25bdb

File tree

6 files changed

+14
-10
lines changed

6 files changed

+14
-10
lines changed

builtin/gc.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static int need_to_gc(void)
220220
static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
221221
{
222222
static struct lock_file lock;
223-
char my_host[128];
223+
char my_host[HOST_NAME_MAX + 1];
224224
struct strbuf sb = STRBUF_INIT;
225225
struct stat st;
226226
uintmax_t pid;
@@ -239,8 +239,12 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
239239
fd = hold_lock_file_for_update(&lock, pidfile_path,
240240
LOCK_DIE_ON_ERROR);
241241
if (!force) {
242-
static char locking_host[128];
242+
static char locking_host[HOST_NAME_MAX + 1];
243+
static char *scan_fmt;
243244
int should_exit;
245+
246+
if (!scan_fmt)
247+
scan_fmt = xstrfmt("%s %%%dc", "%"SCNuMAX, HOST_NAME_MAX);
244248
fp = fopen(pidfile_path, "r");
245249
memset(locking_host, 0, sizeof(locking_host));
246250
should_exit =
@@ -256,7 +260,7 @@ static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
256260
* running.
257261
*/
258262
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 &&
260264
/* be gentle to concurrent "gc" on remote hosts */
261265
(strcmp(locking_host, my_host) || !kill(pid, 0) || errno == EPERM);
262266
if (fp != NULL)

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ static const char *unpack(int err_fd, struct shallow_info *si)
16551655
if (status)
16561656
return "unpack-objects abnormal exit";
16571657
} else {
1658-
char hostname[256];
1658+
char hostname[HOST_NAME_MAX + 1];
16591659

16601660
argv_array_pushl(&child.args, "index-pack",
16611661
"--stdin", hdr_arg, NULL);

daemon.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#include "strbuf.h"
55
#include "string-list.h"
66

7-
#ifndef HOST_NAME_MAX
8-
#define HOST_NAME_MAX 256
9-
#endif
10-
117
#ifdef NO_INITGROUPS
128
#define initgroups(x, y) (0) /* nothing */
139
#endif

fetch-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ static int get_pack(struct fetch_pack_args *args,
745745
if (args->use_thin_pack)
746746
argv_array_push(&cmd.args, "--fix-thin");
747747
if (args->lock_pack || unpack_limit) {
748-
char hostname[256];
748+
char hostname[HOST_NAME_MAX + 1];
749749
if (gethostname(hostname, sizeof(hostname)))
750750
xsnprintf(hostname, sizeof(hostname), "localhost");
751751
argv_array_pushf(&cmd.args,

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,10 @@ static inline size_t xsize_t(off_t len)
878878
__attribute__((format (printf, 3, 4)))
879879
extern int xsnprintf(char *dst, size_t max, const char *fmt, ...);
880880

881+
#ifndef HOST_NAME_MAX
882+
#define HOST_NAME_MAX 256
883+
#endif
884+
881885
/* in ctype.c, for kwset users */
882886
extern const unsigned char tolower_trans_tbl[256];
883887

ident.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ static int canonical_name(const char *host, struct strbuf *out)
120120

121121
static void add_domainname(struct strbuf *out, int *is_bogus)
122122
{
123-
char buf[1024];
123+
char buf[HOST_NAME_MAX + 1];
124124

125125
if (gethostname(buf, sizeof(buf))) {
126126
warning_errno("cannot get host name");

0 commit comments

Comments
 (0)