]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
extract-word: move start block outside the for loop
authorFilipe Brandenburger <filbranden@google.com>
Wed, 4 Nov 2015 04:13:11 +0000 (20:13 -0800)
committerFilipe Brandenburger <filbranden@google.com>
Fri, 6 Nov 2015 05:19:54 +0000 (21:19 -0800)
This block runs once before all the other handling, so move it outside
the main loop and put it in its own loop until it's finished doing its
job.

Tested by confirming `make check` (and particularly test-extract-word)
still passes and by booting a system with binaries including this
commit.

src/basic/extract-word.c

index 6721b85c0ad396fc77b01bb9becdce469a4bed32..23e3d557c03be0a052386c5f27bc6600137fb284 100644 (file)
 int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags) {
         _cleanup_free_ char *s = NULL;
         size_t allocated = 0, sz = 0;
+        char c;
         int r;
 
         char quote = 0;                 /* 0 or ' or " */
         bool backslash = false;         /* whether we've just seen a backslash */
         bool separator = false;         /* whether we've just seen a separator */
-        bool start = true;              /* false means we're looking at a value */
 
         assert(p);
         assert(ret);
@@ -51,31 +51,30 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
          * (because of an uneven number of quotes or similar), leaves
          * the pointer *p at the first invalid character. */
 
-        for (;;) {
-                char c = **p;
+        if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
+                if (!GREEDY_REALLOC(s, allocated, sz+1))
+                        return -ENOMEM;
 
-                if (start) {
+        for (;;) {
+                c = **p;
+                if (c == 0)
+                        goto finish_force_terminate;
+                else if (strchr(separators, c)) {
+                        (*p) ++;
                         if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
-                                if (!GREEDY_REALLOC(s, allocated, sz+1))
-                                        return -ENOMEM;
-
-                        if (c == 0)
-                                goto finish_force_terminate;
-                        else if (strchr(separators, c)) {
-                                (*p) ++;
-                                if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
-                                        goto finish_force_next;
-                                continue;
-                        }
-
+                                goto finish_force_next;
+                } else {
                         /* We found a non-blank character, so we will always
                          * want to return a string (even if it is empty),
                          * allocate it here. */
                         if (!GREEDY_REALLOC(s, allocated, sz+1))
                                 return -ENOMEM;
-
-                        start = false;
+                        break;
                 }
+        }
+
+        for (;;) {
+                c = **p;
 
                 if (backslash) {
                         if (!GREEDY_REALLOC(s, allocated, sz+7))