From d3ef6c5560613fe98fcd227b0256ab4af6078ad0 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Sun, 30 Aug 2015 19:16:50 -0700 Subject: 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 --- src/basic/util.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/basic/util.c b/src/basic/util.c index f752595ca1..fc0f000848 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -5769,25 +5769,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; -- cgit v1.2.3-54-g00ecf From 8ab00959fb9758510c7b88bb1e8cf03162361e15 Mon Sep 17 00:00:00 2001 From: Filipe Brandenburger Date: Sun, 30 Aug 2015 19:40:44 -0700 Subject: extract_first_word: Refactor EXTRACT_DONT_COALESCE_SEPARATORS handling Refactor allocation of the result string to the top, since it is currently done in both branches of the condition. Remove unreachable code checking for EXTRACT_DONT_COALESCE_SEPARATORS when state == SEPARATOR (the only place where SEPARATOR is assigned to state follows a check for EXTRACT_DONT_COALESCE_SEPARATORS that jumps to the end of the function.) Tested by running test-util successfully. Follow up to: 206644aedeb8859801051ac170ec562c6a113a79 --- src/basic/util.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/basic/util.c b/src/basic/util.c index fc0f000848..a9071aa117 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -5754,15 +5754,14 @@ int extract_first_word(const char **p, char **ret, const char *separators, Extra switch (state) { case START: - if (c == 0) { - if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) - if (!GREEDY_REALLOC(s, allocated, sz+1)) - return -ENOMEM; + 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)) { + else if (strchr(separators, c)) { if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) { - if (!GREEDY_REALLOC(s, allocated, sz+1)) - return -ENOMEM; (*p) ++; goto finish_force_next; } @@ -5891,8 +5890,6 @@ end_escape: case SEPARATOR: if (c == 0) goto finish_force_terminate; - if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) - goto finish_force_next; if (!strchr(separators, c)) goto finish; break; -- cgit v1.2.3-54-g00ecf