From: Armaan Sandhu <74664101+Ar-maan05@users.noreply.github.com> Date: Mon, 20 Jul 2026 07:35:12 +0000 (+0530) Subject: hostname-util: strip all trailing separators in hostname_cleanup() (#43077) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=79f25def86c5cc7b52f94ab6bcccf1aac241d471;p=thirdparty%2Fsystemd.git hostname-util: strip all trailing separators in hostname_cleanup() (#43077) Fixes #43054. --- diff --git a/src/basic/hostname-util.c b/src/basic/hostname-util.c index 9410dd05d23..ea49676ea30 100644 --- a/src/basic/hostname-util.c +++ b/src/basic/hostname-util.c @@ -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--; diff --git a/src/test/test-hostname-util.c b/src/test/test-hostname-util.c index 513c8edc9b8..d2b590197cf 100644 --- a/src/test/test-hostname-util.c +++ b/src/test/test-hostname-util.c @@ -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");