]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
unquote_first_word: set *p=NULL on termination
authorRichard Maw <richard.maw@codethink.co.uk>
Fri, 19 Jun 2015 16:38:06 +0000 (16:38 +0000)
committerRichard Maw <richard.maw@codethink.co.uk>
Fri, 7 Aug 2015 15:50:42 +0000 (15:50 +0000)
To add a flag to allow an empty string to be parsed as an argument, we
need to be able to distinguish between the end of the string, and after
the end of the string, so when we *do* reach the end, let's set *p to
this state.

src/basic/util.c

index d4d3d3c83a88536aab3c5e57c6f440753fab48d3..bb7ec007d791c58c212a31d6c7ed9223496a4edc 100644 (file)
@@ -5715,9 +5715,12 @@ int unquote_first_word(const char **p, char **ret, UnquoteFlags flags) {
         } state = START;
 
         assert(p);
-        assert(*p);
         assert(ret);
 
+        /* Bail early if called after last value or with no input */
+        if (!*p)
+                goto finish_force_terminate;
+
         /* Parses the first word of a string, and returns it in
          * *ret. Removes all quotes in the process. When parsing fails
          * (because of an uneven number of quotes or similar), leaves
@@ -5730,7 +5733,7 @@ int unquote_first_word(const char **p, char **ret, UnquoteFlags flags) {
 
                 case START:
                         if (c == 0)
-                                goto finish;
+                                goto finish_force_terminate;
                         else if (strchr(WHITESPACE, c))
                                 break;
 
@@ -5739,7 +5742,7 @@ int unquote_first_word(const char **p, char **ret, UnquoteFlags flags) {
 
                 case VALUE:
                         if (c == 0)
-                                goto finish;
+                                goto finish_force_terminate;
                         else if (c == '\'') {
                                 if (!GREEDY_REALLOC(s, allocated, sz+1))
                                         return -ENOMEM;
@@ -5766,7 +5769,7 @@ int unquote_first_word(const char **p, char **ret, UnquoteFlags flags) {
                 case SINGLE_QUOTE:
                         if (c == 0) {
                                 if (flags & UNQUOTE_RELAX)
-                                        goto finish;
+                                        goto finish_force_terminate;
                                 return -EINVAL;
                         } else if (c == '\'')
                                 state = VALUE;
@@ -5814,10 +5817,10 @@ int unquote_first_word(const char **p, char **ret, UnquoteFlags flags) {
                                          * mode, UNQUOTE_CUNESCAP_RELAX mode does not allow them.
                                          */
                                         s[sz++] = '\\';
-                                        goto finish;
+                                        goto finish_force_terminate;
                                 }
                                 if (flags & UNQUOTE_RELAX)
-                                        goto finish;
+                                        goto finish_force_terminate;
                                 return -EINVAL;
                         }
 
@@ -5861,8 +5864,11 @@ end_escape:
                 (*p) ++;
         }
 
+finish_force_terminate:
+        *p = NULL;
 finish:
         if (!s) {
+                *p = NULL;
                 *ret = NULL;
                 return 0;
         }