]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hostname-util: strip all trailing separators in hostname_cleanup() (#43077)
authorArmaan Sandhu <74664101+Ar-maan05@users.noreply.github.com>
Mon, 20 Jul 2026 07:35:12 +0000 (13:05 +0530)
committerGitHub <noreply@github.com>
Mon, 20 Jul 2026 07:35:12 +0000 (16:35 +0900)
Fixes #43054.

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

index 9410dd05d23f9f988b37c5c38d335e980e72e721..ea49676ea304e57aa5188cf6d43bacf844f245de 100644 (file)
@@ -132,7 +132,7 @@ char* hostname_cleanup(char *s) {
                         hyphen = false;
                 }
 
-        if (d > s && IN_SET(d[-1], '-', '.'))
+        while (d > s && IN_SET(d[-1], '-', '.'))
                 /* The dot can occur at most once, but we might have multiple
                  * hyphens, hence the loop */
                 d--;
index 513c8edc9b83cdd4a314d48b7d74ec9e9616adbe..d2b590197cf3dc5e4f8de6228c868dab9d9a6b4e 100644 (file)
@@ -85,6 +85,14 @@ TEST(hostname_cleanup) {
         ASSERT_STREQ(hostname_cleanup(s), "foo.bar");
         s = strdupa_safe("foo.bar..");
         ASSERT_STREQ(hostname_cleanup(s), "foo.bar");
+        s = strdupa_safe("foobar--");
+        ASSERT_STREQ(hostname_cleanup(s), "foobar");
+        s = strdupa_safe("foobar-.");
+        ASSERT_STREQ(hostname_cleanup(s), "foobar");
+        s = strdupa_safe("foobar.-");
+        ASSERT_STREQ(hostname_cleanup(s), "foobar");
+        s = strdupa_safe("foobar-.-");
+        ASSERT_STREQ(hostname_cleanup(s), "foobar");
         s = strdupa_safe("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
         ASSERT_STREQ(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
         s = strdupa_safe("xxxx........xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");