]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: use strv_split_colon_pairs when parsing RootImageOptions
authorLuca Boccassi <luca.boccassi@microsoft.com>
Thu, 6 Aug 2020 18:43:22 +0000 (19:43 +0100)
committerLuca Boccassi <bluca@debian.org>
Thu, 20 Aug 2020 12:24:32 +0000 (13:24 +0100)
src/core/load-fragment.c
src/shared/bus-unit-util.c

index 266382c84c7289c8b981f9903a75a1b6dd6b94b3..aed5674f2f6fdda6148f26e763faeb5720e6a96e 100644 (file)
@@ -1429,9 +1429,10 @@ int config_parse_root_image_options(
                 void *userdata) {
 
         _cleanup_(mount_options_free_allp) MountOptions *options = NULL;
+        _cleanup_strv_free_ char **l = NULL;
+        char **first = NULL, **second = NULL;
         ExecContext *c = data;
         const Unit *u = userdata;
-        const char *p = rvalue;
         int r;
 
         assert(filename);
@@ -1444,43 +1445,30 @@ int config_parse_root_image_options(
                 return 0;
         }
 
-        for (;;) {
-                _cleanup_free_ char *mount_options_resolved = NULL, *first = NULL, *tuple = NULL;
-                const char *mount_options = NULL, *second = NULL;
+        r = strv_split_colon_pairs(&l, rvalue);
+        if (r == -ENOMEM)
+                return log_oom();
+        if (r < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s, ignoring: %s", lvalue, rvalue);
+                return 0;
+        }
+
+        STRV_FOREACH_PAIR(first, second, l) {
+                _cleanup_free_ char *mount_options_resolved = NULL;
+                const char *mount_options = NULL;
                 MountOptions *o = NULL;
                 unsigned int partition_number = 0;
 
-                r = extract_first_word(&p, &tuple, WHITESPACE, EXTRACT_UNQUOTE);
-                if (r == 0)
-                        break;
-                if (r == -ENOMEM)
-                        return log_oom();
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue);
-                        return 0;
-                }
-
-                second = tuple;
-                r = extract_first_word(&second, &first, ":", EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
-                if (r == 0)
-                        continue;
-                if (r == -ENOMEM)
-                        return log_oom();
-                if (r < 0) {
-                        log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse %s: %s", lvalue, rvalue);
-                        continue;
-                }
-
                 /* Format is either '0:foo' or 'foo' (0 is implied) */
-                if (!isempty(second) && second[-1] == ':') {
-                        mount_options = second;
-                        r = safe_atou(first, &partition_number);
+                if (!isempty(*second)) {
+                        mount_options = *second;
+                        r = safe_atou(*first, &partition_number);
                         if (r < 0) {
-                                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition number from \"%s\", ignoring: %m", first);
+                                log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse partition number from \"%s\", ignoring: %m", *first);
                                 continue;
                         }
                 } else
-                        mount_options = first;
+                        mount_options = *first;
 
                 r = unit_full_printf(u, mount_options, &mount_options_resolved);
                 if (r < 0) {
index 3f4ab65af875e1224851d08330d8f5480b304fcd..d307e746f157c3b72d1364c444a175b714847c2d 100644 (file)
@@ -1456,6 +1456,8 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
         }
 
         if (streq(field, "RootImageOptions")) {
+                _cleanup_strv_free_ char **l = NULL;
+                char **first = NULL, **second = NULL;
                 const char *p = eq;
 
                 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, "sv");
@@ -1474,34 +1476,24 @@ static int bus_append_execute_property(sd_bus_message *m, const char *field, con
                 if (r < 0)
                         return bus_log_create_error(r);
 
-                for (;;) {
-                        _cleanup_free_ char *first = NULL, *tuple = NULL;
-                        const char *mount_options = NULL, *second = NULL;
-                        unsigned partition_number = 0;
-
-                        r = extract_first_word(&p, &tuple, WHITESPACE, EXTRACT_UNQUOTE);
-                        if (r == 0)
-                                break;
-                        if (r < 0)
-                                return log_error_errno(r, "Failed to parse argument: %m");
+                r = strv_split_colon_pairs(&l, p);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to parse argument: %m");
 
-                        second = tuple;
-                        r = extract_first_word(&second, &first, ":", EXTRACT_UNQUOTE|EXTRACT_DONT_COALESCE_SEPARATORS);
-                        if (r == 0)
-                                continue;
-                        if (r < 0)
-                                return log_error_errno(r, "Failed to parse argument: %m");
+                STRV_FOREACH_PAIR(first, second, l) {
+                        const char *mount_options;
+                        unsigned partition_number = 0;
 
                         /* Format is either '0:foo' or 'foo' (0 is implied) */
-                        if (!isempty(second) && second[-1] == ':') {
-                                mount_options = second;
-                                r = safe_atou(first, &partition_number);
+                        if (!isempty(*second)) {
+                                mount_options = *second;
+                                r = safe_atou(*first, &partition_number);
                                 if (r < 0) {
-                                        log_error_errno(r, "Failed to parse partition number from %s: %m", first);
+                                        log_error_errno(r, "Failed to parse partition number from %s: %m", *first);
                                         continue;
                                 }
                         } else
-                                mount_options = first;
+                                mount_options = *first;
 
                         r = sd_bus_message_append(m, "(us)", partition_number, mount_options);
                         if (r < 0)