hostname-util: strip all trailing separators in hostname_cleanup() (#43077)

Fixes #43054.
This commit is contained in:
Armaan Sandhu
2026-07-20 13:05:12 +05:30
committed by GitHub
parent 13aec9fddf
commit 79f25def86
2 changed files with 9 additions and 1 deletions

View 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--;

View 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");