]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: check validity before copying the input string
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 16 Aug 2021 03:48:04 +0000 (12:48 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 16 Aug 2021 04:48:15 +0000 (13:48 +0900)
Prompted by https://github.com/systemd/systemd/pull/20440#discussion_r689136337.

src/network/networkd-network.c

index 0f4b00992fb09e18d984cd402a58c1ac681937b9..5d287f7751a10be052b44c7654dd0b8ffecaa722 100644 (file)
@@ -861,26 +861,26 @@ int config_parse_hostname(
                 void *data,
                 void *userdata) {
 
-        _cleanup_free_ char *hn = NULL;
         char **hostname = data;
         int r;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
-        assert(hostname);
+        assert(data);
 
-        r = config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &hn, userdata);
-        if (r < 0)
-                return r;
+        if (isempty(rvalue)) {
+                *hostname = mfree(*hostname);
+                return 0;
+        }
 
-        if (!hostname_is_valid(hn, 0)) {
+        if (!hostname_is_valid(rvalue, 0)) {
                 log_syntax(unit, LOG_WARNING, filename, line, 0,
                            "Hostname is not valid, ignoring assignment: %s", rvalue);
                 return 0;
         }
 
-        r = dns_name_is_valid(hn);
+        r = dns_name_is_valid(rvalue);
         if (r < 0) {
                 log_syntax(unit, LOG_WARNING, filename, line, r,
                            "Failed to check validity of hostname '%s', ignoring assignment: %m", rvalue);
@@ -892,7 +892,7 @@ int config_parse_hostname(
                 return 0;
         }
 
-        return free_and_replace(*hostname, hn);
+        return free_and_strdup_warn(hostname, rvalue);
 }
 
 int config_parse_timezone(
@@ -907,26 +907,27 @@ int config_parse_timezone(
                 void *data,
                 void *userdata) {
 
-        _cleanup_free_ char *tz = NULL;
-        char **datap = data;
+        char **tz = data;
         int r;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
-        assert(datap);
+        assert(data);
 
-        r = config_parse_string(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &tz, userdata);
-        if (r < 0)
-                return r;
+        if (isempty(rvalue)) {
+                *tz = mfree(*tz);
+                return 0;
+        }
 
-        if (!timezone_is_valid(tz, LOG_WARNING)) {
-                log_syntax(unit, LOG_WARNING, filename, line, 0,
+        r = verify_timezone(rvalue, LOG_WARNING);
+        if (r < 0) {
+                log_syntax(unit, LOG_WARNING, filename, line, r,
                            "Timezone is not valid, ignoring assignment: %s", rvalue);
                 return 0;
         }
 
-        return free_and_replace(*datap, tz);
+        return free_and_strdup_warn(tz, rvalue);
 }
 
 int config_parse_dns(