From d2e727bf6826e486e9058877332860314c760df8 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sun, 15 Jun 2025 19:13:22 +0200 Subject: [PATCH] hostname-util: do not allow empty machine spec Follow-up for 2ae32e9d8fc95010ee4b52b3118ea9fbf05d96d6 Let's not open this up even further and rather disallow --machine="". --- src/basic/hostname-util.c | 16 ++++++++++++---- src/test/test-hostname-util.c | 3 ++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 1372dec0b2f..047f0c07355 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -179,6 +179,8 @@ int split_user_at_host(const char *s, char **ret_user, char **ret_host) { /* Splits a user@host expression (one of those we accept on --machine= and similar). Returns NULL in * each of the two return parameters if that part was left empty. */ + assert(s); + const char *rhs = strchr(s, '@'); if (rhs) { if (ret_user && rhs > s) { @@ -192,10 +194,16 @@ int split_user_at_host(const char *s, char **ret_user, char **ret_host) { if (!h) return -ENOMEM; } - } else if (!isempty(s) && ret_host) { - h = strdup(s); - if (!h) - return -ENOMEM; + + } else { + if (isempty(s)) + return -EINVAL; + + if (ret_host) { + h = strdup(s); + if (!h) + return -ENOMEM; + } } if (ret_user) diff --git a/src/test/test-hostname-util.c b/src/test/test-hostname-util.c index 4abac5ca3a9..b3e3b1b84a7 100644 --- a/src/test/test-hostname-util.c +++ b/src/test/test-hostname-util.c @@ -107,7 +107,8 @@ static void test_split_user_at_host_one(const char *s, const char *expected_user } TEST(split_user_at_host) { - test_split_user_at_host_one("", NULL, NULL, 0); + ASSERT_ERROR(split_user_at_host("", NULL, NULL), EINVAL); + test_split_user_at_host_one("@", NULL, NULL, 1); test_split_user_at_host_one("a", NULL, "a", 0); test_split_user_at_host_one("a@b", "a", "b", 1); -- 2.47.3