From df5508eed053c72cda91ae7dfe3d216512695336 Mon Sep 17 00:00:00 2001 From: Arnaud Cornet Date: Sun, 9 Dec 2018 19:52:46 -0500 Subject: [PATCH] Deal more gracefully with long home directories. This is a better fix for: https://projects.duckcorp.org/issues/636 --- src/bip.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/bip.c b/src/bip.c index 47f7f4d..e6fd48d 100644 --- a/src/bip.c +++ b/src/bip.c @@ -163,9 +163,11 @@ void conf_die(bip_t *bip, char *fmt, ...) int do_pid_stuff(void) { char hname[512]; - char longpath[1024]; FILE *f; int fd; + // size is conf_pid_file + hname max + %ld max + two '.'. + int longpath_max = strlen(conf_pid_file) + 512 + 3 + 20; + char *longpath = bip_malloc(longpath_max + 1); try_again: fd = -1; @@ -175,9 +177,9 @@ try_again: if (gethostname(hname, 511) == -1) fatal("%s %s", "gethostname", strerror(errno)); hname[511] = 0; - snprintf(longpath, 1023, "%s.%s.%ld", conf_pid_file, hname, + snprintf(longpath, longpath_max - 1, "%s.%s.%ld", conf_pid_file, hname, (long unsigned int)getpid()); - longpath[1023] = 0; + longpath[longpath_max] = 0; if ((fd = open(longpath, O_CREAT|O_WRONLY, S_IWUSR|S_IRUSR)) == -1) fatal("Cannot write to PID file (%s) %s", longpath, strerror(errno)); @@ -191,6 +193,7 @@ try_again: } } unlink(longpath); + free(longpath); return fd; pid_is_there: { @@ -232,6 +235,7 @@ pid_is_there: " %s.", conf_pid_file); exit(2); } + free(longpath); return 0; }