From b85e1c2534ca3b396c2aaa7de384995b42d12e1b Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Tue, 3 Nov 2015 20:13:11 -0800 Subject: [PATCH] extract-word: move start block outside the for loop 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 | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/basic/extract-word.c b/src/basic/extract-word.c index 6721b85c0a..23e3d557c0 100644 --- a/src/basic/extract-word.c +++ b/src/basic/extract-word.c @@ -29,12 +29,12 @@ 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. */ + if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) + if (!GREEDY_REALLOC(s, allocated, sz+1)) + return -ENOMEM; + for (;;) { - char c = **p; - - if (start) { + 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))