summaryrefslogtreecommitdiff
path: root/src/basic/util.c
diff options
context:
space:
mode:
authorFilipe Brandenburger <filbranden@google.com>2015-08-30 19:16:50 -0700
committerSven Eden <yamakuzure@gmx.net>2017-03-14 10:19:06 +0100
commitd58deedf28351f217c1348604eab87ec075975c2 (patch)
treee9d8d8801c9325b6c024e771c7b98b73d3414c14 /src/basic/util.c
parent8875ab3b75fb21bec36f56d08043ab50dfe650f8 (diff)
extract_first_word: Refactor allocation in empty argument case
This covers the case where an argument is an empty string, such as ''. Instead of allocating the empty string in the individual conditions when state == VALUE, just always allocate it at the end of state == START, at which point we know we will have an argument. Tested that test-util keeps passing after the refactor. Follow up to: 14e685c29d5b317b815e3e9f056648027852b07e
Diffstat (limited to 'src/basic/util.c')
-rw-r--r--src/basic/util.c20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/basic/util.c b/src/basic/util.c
index c6b48ef30..fbd6ff5d9 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -5859,25 +5859,25 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra
break;
}
+ /* 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;
+
state = VALUE;
/* fallthrough */
case VALUE:
if (c == 0)
goto finish_force_terminate;
- else if (c == '\'' && (flags & EXTRACT_QUOTES)) {
- if (!GREEDY_REALLOC(s, allocated, sz+1))
- return -ENOMEM;
-
+ else if (c == '\'' && (flags & EXTRACT_QUOTES))
state = SINGLE_QUOTE;
- } else if (c == '\\')
+ else if (c == '\\')
state = VALUE_ESCAPE;
- else if (c == '\"' && (flags & EXTRACT_QUOTES)) {
- if (!GREEDY_REALLOC(s, allocated, sz+1))
- return -ENOMEM;
-
+ else if (c == '\"' && (flags & EXTRACT_QUOTES))
state = DOUBLE_QUOTE;
- } else if (strchr(separators, c)) {
+ else if (strchr(separators, c)) {
if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
(*p) ++;
goto finish_force_next;