Re: [PATCH v3 1/2] sequencer: handle errors in read_author_ident()
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Aug 3, 2018, 07:09 UTC
- Message-ID
- <CAPig+cSZ3Zm=qFcvGjyj_uStn=JMAYuskMa0O_2yxkKjaRWTSg@mail.gmail.com>
- In-Reply-To
- <20180802112002.720-2-phillip.wood@talktalk.net>
On Thu, Aug 2, 2018 at 7:20 AM Phillip Wood <phillip.wood@talktalk.net> wrote:
Show 21 quoted lines
> The calling code did not treat NULL as an error. Instead NULL caused
> it to fallback to using the default author when creating the new
> commit. This changed the date and potentially the author of the
> commit which corrupted the author data compared to its expected
> value. Fix this by returning and integer and passing in a parameter to
> receive the author.
>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---
> diff --git a/sequencer.c b/sequencer.c
> @@ -701,57 +701,59 @@ static char *get_author(const char *message)
> -static const char *read_author_ident(struct strbuf *buf)
> +static int read_author_ident(char **author)
> {
> + struct strbuf buf = STRBUF_INIT;
> - if (strbuf_read_file(buf, rebase_path_author_script(), 256) <= 0)
> - return NULL;
> + if (strbuf_read_file(&buf, rebase_path_author_script(), 256) <= 0) {
> + strbuf_release(&buf);
> + return -1;
> + }[...much noisiness snipped...]
Show 11 quoted lines
> @@ -794,12 +796,14 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
> if ((flags & CREATE_ROOT_COMMIT) && !(flags & AMEND_MSG)) {
> - struct strbuf msg = STRBUF_INIT, script = STRBUF_INIT;
> - const char *author = is_rebase_i(opts) ?
> - read_author_ident(&script) : NULL;
> + struct strbuf msg = STRBUF_INIT;
> + char *author = NULL;
>
> + if (is_rebase_i(opts) && read_author_ident(&author))
> + return -1;
> +I think this patch can be simplified considerably by shifting one's perspective. If we admit that read_author_ident() is already correctly reporting an error by returning NULL (which is exactly what it is doing), then the bug is is purely on the calling side; namely, the caller is ignoring the error. (In fact, your commit message already states this.)
So, if it's the caller which is buggy, then read_author_ident() does not require _any_ changes (meaning all the noisiness in this patch can be dropped), and only the caller needs a fix, and that change can be quite tiny. To wit, instead of initializing 'author' like this (as done in the current "buggy" code):
const char *author = is_rebase_i(opts) ?
read_author_ident(&script) : NULL;Do this instead:
const char *author = NULL;
...
if (is_rebase_i(opts)) {
author = read_author_ident(&script);
if (!author) {
strbuf_release(&script);
return -1;
}
}That's it, a minimal fix giving the same result, without all the code churn, thus safer (less opportunity, for instance, to introduce a leak as in v1). Of course, you can collapse the above even further to:
if (is_rebase_i(opts) &&
!(author = read_author_ident(&script)) {
strbuf_release(&script);
return -1;
}Though, I think that is less readable.