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:
parent
d65652f1f2
commit
5fe7a0a7de
|
@ -143,9 +143,7 @@ char* hostname_cleanup(char *s) {
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
|
|
||||||
strshorten(s, HOST_NAME_MAX);
|
for (p = s, d = s, dot = hyphen = true; *p && d - s < HOST_NAME_MAX; p++)
|
||||||
|
|
||||||
for (p = s, d = s, dot = hyphen = true; *p; p++)
|
|
||||||
if (*p == '.') {
|
if (*p == '.') {
|
||||||
if (dot || hyphen)
|
if (dot || hyphen)
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -85,6 +85,8 @@ static void test_hostname_cleanup(void) {
|
||||||
assert_se(streq(hostname_cleanup(s), "foo.bar"));
|
assert_se(streq(hostname_cleanup(s), "foo.bar"));
|
||||||
s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
|
s = strdupa("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
|
||||||
assert_se(streq(hostname_cleanup(s), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
|
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) {
|
static void test_read_etc_hostname(void) {
|
||||||
|
|
Loading…
Reference in a new issue