Subject: Re: [PATCH v4 1/4] revision: improve commit_rewrite_person()

From: Junio C Hamano <[email-masked]>
Date: Thu, 14 Jul 2022 14:02:50 -0700

Siddharth Asthana <[email-masked]> writes:

> +/*
> + * Returns the difference between the new and old length of the ident line.
> + */
> +static ssize_t rewrite_ident_line(const char* person, struct strbuf *buf, struct string_list *mailmap)

"const char *person".  Asterisk sticks to the variable, not to the
type.

Wrap such an overly long line.

> +static void commit_rewrite_person(struct strbuf *buf, const char **headers, struct string_list *mailmap)

Likewise.

The second parameter should be called "header", not "headers",
because the way this array is used is primarily to access its
individual members, i.e. header[i] and it is more grammatical
to say that header[0] is the zero-th header, not headers[0].

> +{
> +	size_t buf_offset = 0;
> +
> +	if (!mailmap)
> +		return;
> +
> +	for (;;) {
> +		const char *person, *line;
> +		size_t i, linelen;
> +
> +		line = buf->buf + buf_offset;

buf_offset is initialized to 0.  The idea is to keep track of the
position of the current line in buf->buf as the byte offset from the
beginning.  This makes it safe to let rewrite_ident_line()
reallocate buf->buf in the loop.  So "line" points at the beginning
of the current line.

> +		linelen = strchrnul(line, '\n') - line + 1;

This "linelen" counts the LF (or NUL) termination.  Hence ...

> +		if (linelen <= 1)
> +			/* End of header */
> +			return;

... linelen==0 means we are at an empty line.

> +		buf_offset += linelen;

And by adding linelen to buf_offset, we prepare buf_offset to point
at the beginning of the next line.

BUT.

What happens when the buffer has only headers, no body, without an
empty line after the header?  buf_offset will be pointing at the
byte past the final NUL at the end of the buffer.  The next round of
the loop will point line at an invalid piece of memory.

I _think_ that the current generation of high-level tools like "git
commit" and "git tag" may leave an blank line at the end even when
they are told to create a message with no body, but this code should
not depend on that.  An object with no body and without a blank line
after the header is a valid object (cf. fsck.c::verify_headers()).

> +		for (i = 0; headers[i]; i++)
> +			if (skip_prefix(line, headers[i], &person))
> +				buf_offset += rewrite_ident_line(person, buf, mailmap);
> +	}

As the commit_headers[] array the caller gives us does not have
duplicates, as soon as we find a match, we should break out of the
loop, i.e.

		for (i = 0; header[i]; i++) {
			if (skip_prefix(line, header[i], &person))
				buf_offset += rewrite_ident_line(person, buf, maimap);
			break;
		}

> +}
> +
>  static int commit_match(struct commit *commit, struct rev_info *opt)
>  {
>  	int retval;
> @@ -3832,11 +3859,12 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
>  		strbuf_addstr(&buf, message);
>  
>  	if (opt->grep_filter.header_list && opt->mailmap) {
> +		const char *commit_headers[] = { "author ", "committer ", NULL };

It is OK to call the array "commit_headers", because the way we use
the identifier is only to refer to the collection of headers as a
whole.

>  		if (!buf.len)
>  			strbuf_addstr(&buf, message);
>  
> -		commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
> -		commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
> +		commit_rewrite_person(&buf, commit_headers, opt->mailmap);
>  	}
>  
>  	/* Append "fake" message parts as needed */

Thanks.