1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-30 06:25:37 +03:00

hostname: fix off-by-one issue in gethostname()

gethostname() returns null-terminated hostname.

Fixes #20309 and #20417.
This commit is contained in:
Yu Watanabe 2021-08-11 00:12:40 +09:00
parent 4e2a772438
commit ccdf235464

View File

@ -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. */