basic/hostname-util: do truncation last when cleaning up

This allows more of the original name to be used if there are invalid
chars in the beginning.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-12-05 12:27:29 +01:00
parent d65652f1f2
commit 5fe7a0a7de
2 changed files with 3 additions and 3 deletions

View File

@ -143,9 +143,7 @@ char* hostname_cleanup(char *s) {
assert(s);
strshorten(s, HOST_NAME_MAX);
for (p = s, d = s, dot = hyphen = true; *p; p++)
for (p = s, d = s, dot = hyphen = true; *p && d - s < HOST_NAME_MAX; p++)
if (*p == '.') {
if (dot || hyphen)
continue;

View File

@ -85,6 +85,8 @@ static void test_hostname_cleanup(void) {
assert_se(streq(hostname_cleanup(s), "foo.bar"));
s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
assert_se(streq(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
s = strdupa("xxxx........xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
assert_se(streq(hostname_cleanup(s), "xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
}
static void test_read_etc_hostname(void) {