trailer: be stricter in parsing separators

Currently, a line is interpreted to be a trailer line if it contains a separator. Make parsing stricter by requiring the text on the left of the separator, if not the empty string, to be of the "<token><optional whitespace>" form. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Nov 2, 2016 at 10:29 UTC e4319562bc2834096fade432fd90c985b96476db
1 file changed +23 -6
trailer.c
+23 -6
@@ -563,15 +563,32 @@ static int token_matches_item(const char *tok, struct arg_item *item, int tok_le
563 }
564
565 /*
566 - * Return the location of the first separator in line, or -1 if there is no
567 - * separator.
566 + * If the given line is of the form
567 + * "<token><optional whitespace><separator>..." or "<separator>...", return the
568 + * location of the separator. Otherwise, return -1. The optional whitespace
569 + * is allowed there primarily to allow things like "Bug #43" where <token> is
570 + * "Bug" and <separator> is "#".
571 + *
572 + * The separator-starts-line case (in which this function returns 0) is
573 + * distinguished from the non-well-formed-line case (in which this function
574 + * returns -1) because some callers of this function need such a distinction.
575 */
576 static int find_separator(const char *line, const char *separators)
577 {
571 - int loc = strcspn(line, separators);
572 - if (!line[loc])
573 - return -1;
574 - return loc;
578 + int whitespace_found = 0;
579 + const char *c;
580 + for (c = line; *c; c++) {
581 + if (strchr(separators, *c))
582 + return c - line;
583 + if (!whitespace_found && (isalnum(*c) || *c == '-'))
584 + continue;
585 + if (c != line && (*c == ' ' || *c == '\t')) {
586 + whitespace_found = 1;
587 + continue;
588 + }
589 + break;
590 + }
591 + return -1;
592 }
593
594 /*