Skip to content

Commit e431956

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent be5a750 commit e431956

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

trailer.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,32 @@ static int token_matches_item(const char *tok, struct arg_item *item, int tok_le
563563
}
564564

565565
/*
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.
568575
*/
569576
static int find_separator(const char *line, const char *separators)
570577
{
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;
575592
}
576593

577594
/*

0 commit comments

Comments
 (0)