# [PATCH] format-patch: notice failure to open cover letter for writing

4 messages from 2019-02-21 to 2019-02-22. Participants: Junio C Hamano, Jeff King, Duy Nguyen.
Thread: https://gitlist.dev/t/50555

## Junio C Hamano, 2019-02-21 23:50

Subject: [PATCH] format-patch: notice failure to open cover letter for writing
Message-ID: <xmqqlg28snv0.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqlg28snv0.fsf%40gitster-ct.c.googlers.com

```
The make_cover_letter() function is supposed to open a new file for
writing, and let the caller write into it via FILE *rev->diffopt.file
but because the function does not return anything, the caller does not
bother checking the return value.

Make sure it dies, instead of keep going with a NULL output
filestream and relying on it to cause a crash.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/log.c           | 2 +-
 t/t4014-format-patch.sh | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/builtin/log.c b/builtin/log.c
index 3e145fe502..43fc9a07df 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -1047,7 +1047,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
 
 	if (!use_stdout &&
 	    open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
-		return;
+		die(_("Failed to create cover-letter file"));
 
 	log_write_email_headers(rev, head, &pp.after_subject, &need_8bit_cte, 0);
 
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index 909c743c13..b6e2fdbc44 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -589,6 +589,12 @@ test_expect_success 'excessive subject' '
 	ls patches/0004-This-is-an-excessively-long-subject-line-for-a-messa.patch
 '
 
+test_expect_success 'failure to write cover-letter aborts gracefully' '
+	test_when_finished "rmdir 0000-cover-letter.patch" &&
+	mkdir 0000-cover-letter.patch &&
+	test_must_fail git format-patch --no-renames --cover-letter -1
+'
+
 test_expect_success 'cover-letter inherits diff options' '
 	git mv file foo &&
 	git commit -m foo &&

```

## Jeff King, 2019-02-22 06:26

Subject: Re: [PATCH] format-patch: notice failure to open cover letter for writing
Message-ID: <20190222062606.GD10248@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20190222062606.GD10248%40sigill.intra.peff.net
In-Reply-To: <xmqqlg28snv0.fsf@gitster-ct.c.googlers.com>

```
On Thu, Feb 21, 2019 at 03:50:11PM -0800, Junio C Hamano wrote:

> The make_cover_letter() function is supposed to open a new file for
> writing, and let the caller write into it via FILE *rev->diffopt.file
> but because the function does not return anything, the caller does not
> bother checking the return value.
> 
> Make sure it dies, instead of keep going with a NULL output
> filestream and relying on it to cause a crash.

Definitely makes sense.

> diff --git a/builtin/log.c b/builtin/log.c
> index 3e145fe502..43fc9a07df 100644
> --- a/builtin/log.c
> +++ b/builtin/log.c
> @@ -1047,7 +1047,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
>  
>  	if (!use_stdout &&
>  	    open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
> -		return;
> +		die(_("Failed to create cover-letter file"));

I wondered if we should be showing errno here, but it looks like
open_next_file() will give us a more specific error (including errno if
applicable).

I'd suggest s/F/f/ in the message to match our usual style, though I see
this code has a lot of capitalized errors already. :)

> +test_expect_success 'failure to write cover-letter aborts gracefully' '
> +	test_when_finished "rmdir 0000-cover-letter.patch" &&
> +	mkdir 0000-cover-letter.patch &&
> +	test_must_fail git format-patch --no-renames --cover-letter -1
> +'

Cute test. :)

-Peff

```

## Duy Nguyen, 2019-02-22 07:50

Subject: Re: [PATCH] format-patch: notice failure to open cover letter for writing
Message-ID: <CACsJy8A1cqWkomePW8i6veNT+WP2nq_kAZHay74ZgZFsz-d+nw@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8A1cqWkomePW8i6veNT%2BWP2nq_kAZHay74ZgZFsz-d%2Bnw%40mail.gmail.com
In-Reply-To: <20190222062606.GD10248@sigill.intra.peff.net>

```
On Fri, Feb 22, 2019 at 1:28 PM Jeff King <peff@peff.net> wrote:
> > diff --git a/builtin/log.c b/builtin/log.c
> > index 3e145fe502..43fc9a07df 100644
> > --- a/builtin/log.c
> > +++ b/builtin/log.c
> > @@ -1047,7 +1047,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
> >
> >       if (!use_stdout &&
> >           open_next_file(NULL, rev->numbered_files ? NULL : "cover-letter", rev, quiet))
> > -             return;
> > +             die(_("Failed to create cover-letter file"));
>
> I'd suggest s/F/f/ in the message to match our usual style, though I see
> this code has a lot of capitalized errors already. :)

Thanks! The lowercase wins over uppercase [1] 1517 to 327.
Unfortunately it's a tie in this file. But I think it's still good
that new messages stick to lowercase.

[1] counting error/warn/die only with

git grep 'die[_a-z]*(_("[A-Z]\|error[_a-z]*(_("[A-Z]\|warning[_a-z]*(_("[A-Z]'
-- \*.c
-- 
Duy

```

## Junio C Hamano, 2019-02-22 17:54

Subject: Re: [PATCH] format-patch: notice failure to open cover letter for writing
Message-ID: <xmqqo973r9nk.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqo973r9nk.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20190222062606.GD10248@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> I'd suggest s/F/f/ in the message to match our usual style, though I see
> this code has a lot of capitalized errors already. :)

Yup, I'd prepare a prelim clean-up before this step.

>> +test_expect_success 'failure to write cover-letter aborts gracefully' '
>> +	test_when_finished "rmdir 0000-cover-letter.patch" &&
>> +	mkdir 0000-cover-letter.patch &&
>> +	test_must_fail git format-patch --no-renames --cover-letter -1
>> +'
>
> Cute test. :)

Those who can find this "cute" would probably know, but for others,
this is a trick to test codepath for "cannot create a file" error
without requiring SANITY prerequisite.

```
