]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname: fix off-by-one issue in gethostname()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 10 Aug 2021 15:12:40 +0000 (00:12 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 12 Aug 2021 04:48:15 +0000 (13:48 +0900)
gethostname() returns null-terminated hostname.

Fixes #20309 and #20417.

src/shared/hostname-setup.c

index 511aa7d0313cb40e37cc6c23f5a8a21972fd71f6..742174d6c8907c1602e04b6c203e51f3d86dddbd 100644 (file)
 #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. */