]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
musl: hostname-util: introduce LINUX_HOST_NAME_MAX
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 6 Sep 2025 21:16:02 +0000 (06:16 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 12 Nov 2025 23:02:05 +0000 (08:02 +0900)
glibc defines HOST_NAME_MAX as 64 and our code rely on that, but musl
defines the constant as 255. Let's provide our own definition for the
maximum length.

src/basic/hostname-util.c
src/basic/hostname-util.h
src/core/load-fragment.c
src/libsystemd-network/sd-lldp-tx.c
src/shared/hostname-setup.c

index 2238e86d76694c86525466bbf6cb466e809b601c..01434d3d641d2864847a18e6b3174cd946aabac0 100644 (file)
@@ -94,8 +94,8 @@ bool hostname_is_valid(const char *s, ValidHostnameFlags flags) {
         if (hyphen)
                 return false;
 
-        if (p-s > HOST_NAME_MAX) /* Note that HOST_NAME_MAX is 64 on Linux, but DNS allows domain names up to
-                                  * 255 characters */
+        /* Note that host name max is 64 on Linux, but DNS allows domain names up to 255 characters. */
+        if (p - s > (ssize_t) LINUX_HOST_NAME_MAX)
                 return false;
 
         return true;
@@ -107,7 +107,7 @@ char* hostname_cleanup(char *s) {
 
         assert(s);
 
-        for (p = s, d = s, dot = hyphen = true; *p && d - s < HOST_NAME_MAX; p++)
+        for (p = s, d = s, dot = hyphen = true; *p && d - s < (ssize_t) LINUX_HOST_NAME_MAX; p++)
                 if (*p == '.') {
                         if (dot || hyphen)
                                 continue;
index e9e605cab0723055ca383f93ee8ceaa8d8585537..73cd83fbb8d6e891bf05e1ab5636d57a4853caf4 100644 (file)
@@ -4,6 +4,9 @@
 #include "basic-forward.h"
 #include "strv.h"
 
+/* HOST_NAME_MAX should be 64 on linux, but musl uses the one by POSIX (255). */
+#define LINUX_HOST_NAME_MAX CONST_MIN((size_t) HOST_NAME_MAX, (size_t) 64)
+
 char* get_default_hostname_raw(void);
 
 bool valid_ldh_char(char c) _const_;
index 2f212b6e7a127a744d5b9df508e648bbbfc8316d..4b33d918fceb33e3ead6b68cd2e48509b4744ebe 100644 (file)
@@ -6633,7 +6633,7 @@ int config_parse_protect_hostname(
 
         const char *colon = strchr(rvalue, ':');
         if (colon) {
-                r = unit_full_printf_full(u, colon + 1, HOST_NAME_MAX, &h);
+                r = unit_full_printf_full(u, colon + 1, LINUX_HOST_NAME_MAX, &h);
                 if (r < 0) {
                         log_syntax(unit, LOG_WARNING, filename, line, r,
                                    "Failed to resolve unit specifiers in '%s', ignoring: %m", colon + 1);
index e8e1ea10f33f60082416f0b58de5b93a858f1f7e..f2f08e40ef817341584f9822dabd6b14bb6acaf3 100644 (file)
@@ -186,12 +186,8 @@ int sd_lldp_tx_set_hostname(sd_lldp_tx *lldp_tx, const char *hostname) {
         assert_return(lldp_tx, -EINVAL);
 
         /* An empty string unset the previously set hostname. */
-        if (!isempty(hostname)) {
-                assert_cc(HOST_NAME_MAX < 512);
-
-                if (!hostname_is_valid(hostname, 0))
-                        return -EINVAL;
-        }
+        if (!isempty(hostname) && !hostname_is_valid(hostname, /* flags= */ 0))
+                return -EINVAL;
 
         return free_and_strdup(&lldp_tx->hostname, empty_to_null(hostname));
 }
index 9e83445040eb2e0a805776ab608eb85984987cc3..0eb05d3714facae063be943b22d37f99ba3e8e45 100644 (file)
@@ -51,7 +51,7 @@ int sethostname_idempotent(const char *s) {
 int shorten_overlong(const char *s, char **ret) {
         _cleanup_free_ char *h = NULL;
 
-        /* Shorten an overlong name to HOST_NAME_MAX or to the first dot,
+        /* Shorten an overlong name to LINUX_HOST_NAME_MAX or to the first dot,
          * whatever comes earlier. */
 
         assert(s);
@@ -70,7 +70,7 @@ int shorten_overlong(const char *s, char **ret) {
         if (p)
                 *p = 0;
 
-        strshorten(h, HOST_NAME_MAX);
+        strshorten(h, LINUX_HOST_NAME_MAX);
 
         if (!hostname_is_valid(h, /* flags= */ 0))
                 return -EDOM;
@@ -403,7 +403,7 @@ int pidref_gethostname_full(PidRef *pidref, GetHostnameFlags flags, char **ret)
         if (r < 0)
                 return r;
 
-        char buf[HOST_NAME_MAX+1];
+        char buf[LINUX_HOST_NAME_MAX+1];
         ssize_t n = loop_read(result_pipe[0], buf, sizeof(buf), /* do_poll = */ false);
         if (n < 0)
                 return n;