parse_arg(): really test that argument is properly terminated
[gitweb.git] / builtin / update-ref.c
index 702e90db2a82a0eba2ff233b9ec46690da5afc3e..02b5f950e34a3122f55f8aba35f3fb116cc57e06 100644 (file)
@@ -62,16 +62,26 @@ static void update_store_old_sha1(struct ref_update *update,
        update->have_old = *oldvalue || line_termination;
 }
 
+/*
+ * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
+ * and append the result to arg.  Return a pointer to the terminator.
+ * Die if there is an error in how the argument is C-quoted.  This
+ * function is only used if not -z.
+ */
 static const char *parse_arg(const char *next, struct strbuf *arg)
 {
-       /* Parse SP-terminated, possibly C-quoted argument */
-       if (*next != '"')
+       if (*next == '"') {
+               const char *orig = next;
+
+               if (unquote_c_style(arg, next, &next))
+                       die("badly quoted argument: %s", orig);
+               if (*next && !isspace(*next))
+                       die("unexpected character after quoted argument: %s", orig);
+       } else {
                while (*next && !isspace(*next))
                        strbuf_addch(arg, *next++);
-       else if (unquote_c_style(arg, next, &next))
-               die("badly quoted argument: %s", next);
+       }
 
-       /* Return position after the argument */
        return next;
 }
 
@@ -229,15 +239,15 @@ static void update_refs_stdin(void)
                        die("empty command in input");
                else if (isspace(*cmd.buf))
                        die("whitespace before command: %s", cmd.buf);
-               else if (!prefixcmp(cmd.buf, "update "))
+               else if (starts_with(cmd.buf, "update "))
                        parse_cmd_update(cmd.buf + 7);
-               else if (!prefixcmp(cmd.buf, "create "))
+               else if (starts_with(cmd.buf, "create "))
                        parse_cmd_create(cmd.buf + 7);
-               else if (!prefixcmp(cmd.buf, "delete "))
+               else if (starts_with(cmd.buf, "delete "))
                        parse_cmd_delete(cmd.buf + 7);
-               else if (!prefixcmp(cmd.buf, "verify "))
+               else if (starts_with(cmd.buf, "verify "))
                        parse_cmd_verify(cmd.buf + 7);
-               else if (!prefixcmp(cmd.buf, "option "))
+               else if (starts_with(cmd.buf, "option "))
                        parse_cmd_option(cmd.buf + 7);
                else
                        die("unknown command: %s", cmd.buf);