From: Yu Watanabe Date: Tue, 10 Aug 2021 15:12:40 +0000 (+0900) Subject: hostname: fix off-by-one issue in gethostname() X-Git-Tag: v250-rc1~839^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ccdf235464297c2ca4c1dea8733a6bad423084d5;p=thirdparty%2Fsystemd.git hostname: fix off-by-one issue in gethostname() gethostname() returns null-terminated hostname. Fixes #20309 and #20417. --- diff --git a/src/shared/hostname-setup.c b/src/shared/hostname-setup.c index 511aa7d0313..742174d6c89 100644 --- a/src/shared/hostname-setup.c +++ b/src/shared/hostname-setup.c @@ -20,11 +20,11 @@ #include "util.h" static int sethostname_idempotent_full(const char *s, bool really) { - char buf[HOST_NAME_MAX + 1] = {}; + char buf[HOST_NAME_MAX + 1]; assert(s); - if (gethostname(buf, sizeof(buf) - 1) < 0) + if (gethostname(buf, sizeof(buf)) < 0) return -errno; if (streq(buf, s)) @@ -42,11 +42,11 @@ int sethostname_idempotent(const char *s) { } bool get_hostname_filtered(char ret[static HOST_NAME_MAX + 1]) { - char buf[HOST_NAME_MAX + 1] = {}; + char buf[HOST_NAME_MAX + 1]; /* Returns true if we got a good hostname, false otherwise. */ - if (gethostname(buf, sizeof(buf) - 1) < 0) + if (gethostname(buf, sizeof(buf)) < 0) return false; /* This can realistically only fail with ENAMETOOLONG. * Let's treat that case the same as an invalid hostname. */