Re: [PATCH v3 1/2] sequencer: handle errors in read_author_ident()

2018-08-03 Thread Junio C Hamano
Eric Sunshine  writes:

> 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.)

This approach looks quite sensible.


Re: [PATCH v3 1/2] sequencer: handle errors in read_author_ident()

2018-08-03 Thread Eric Sunshine
On Thu, Aug 2, 2018 at 7:20 AM Phillip Wood  wrote:
> 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 
> ---
> 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(, rebase_path_author_script(), 256) <= 0) {
> +   strbuf_release();
> +   return -1;
> +   }

[...much noisiness snipped...]

> @@ -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() : NULL;
> +   struct strbuf msg = STRBUF_INIT;
> +   char *author = NULL;
>
> +   if (is_rebase_i(opts) && read_author_ident())
> +   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() : NULL;

Do this instead:

const char *author = NULL;
...
if (is_rebase_i(opts)) {
author = read_author_ident();
if (!author) {
strbuf_release();
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()) {
strbuf_release();
return -1;
}

Though, I think that is less readable.