mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-11 05:17:44 +03:00
hostname: fix off-by-one issue in gethostname()
gethostname() returns null-terminated hostname. Fixes #20309 and #20417.
This commit is contained in:
parent
4e2a772438
commit
ccdf235464
@ -20,11 +20,11 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static int sethostname_idempotent_full(const char *s, bool really) {
|
static int sethostname_idempotent_full(const char *s, bool really) {
|
||||||
char buf[HOST_NAME_MAX + 1] = {};
|
char buf[HOST_NAME_MAX + 1];
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
|
|
||||||
if (gethostname(buf, sizeof(buf) - 1) < 0)
|
if (gethostname(buf, sizeof(buf)) < 0)
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
if (streq(buf, s))
|
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]) {
|
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. */
|
/* 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.
|
return false; /* This can realistically only fail with ENAMETOOLONG.
|
||||||
* Let's treat that case the same as an invalid hostname. */
|
* Let's treat that case the same as an invalid hostname. */
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user