Skip to content

Commit 871d21d

Browse files
bebarinogitster
authored andcommitted
format_sanitized_subject: Don't trim past initial length of strbuf
If the subject line is '...' the strbuf will be accessed before the first dot is added; potentially changing the strbuf passed into the function or accessing sb->buf[-1] if it was originally empty. Reported-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b09b868 commit 871d21d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pretty.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ static int istitlechar(char c)
502502
static void format_sanitized_subject(struct strbuf *sb, const char *msg)
503503
{
504504
size_t trimlen;
505+
size_t start_len = sb->len;
505506
int space = 2;
506507

507508
for (; *msg && *msg != '\n'; msg++) {
@@ -519,8 +520,9 @@ static void format_sanitized_subject(struct strbuf *sb, const char *msg)
519520

520521
/* trim any trailing '.' or '-' characters */
521522
trimlen = 0;
522-
while (sb->buf[sb->len - 1 - trimlen] == '.'
523-
|| sb->buf[sb->len - 1 - trimlen] == '-')
523+
while (sb->len - trimlen > start_len &&
524+
(sb->buf[sb->len - 1 - trimlen] == '.'
525+
|| sb->buf[sb->len - 1 - trimlen] == '-'))
524526
trimlen++;
525527
strbuf_remove(sb, sb->len - trimlen, trimlen);
526528
}

0 commit comments

Comments
 (0)