]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname-util: do not allow empty machine spec
authorMike Yuan <me@yhndnzj.com>
Sun, 15 Jun 2025 17:13:22 +0000 (19:13 +0200)
committerMike Yuan <me@yhndnzj.com>
Mon, 16 Jun 2025 23:39:56 +0000 (01:39 +0200)
Follow-up for 2ae32e9d8fc95010ee4b52b3118ea9fbf05d96d6

Let's not open this up even further and rather disallow
--machine="".

src/basic/hostname-util.c
src/test/test-hostname-util.c

index 1372dec0b2f7f5c4af3b08bdd8c75fc952c1ad97..047f0c07355f015d9af65a93cd01c2a6190538da 100644 (file)
@@ -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)
index 4abac5ca3a95b50f4b1729008e7da4e28e94b471..b3e3b1b84a753901c7d3051ec0351cd9a03fffe0 100644 (file)
@@ -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);