git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] mailinfo.c: move side-effects outside of assert

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 19, 2016, 23:26 UTC
Message-ID
<xmqqbmw7mrg4.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<ebaf4c892a78bc3ae614a23d87f9c0f@58437222ff6db9ee7cbe9d1a5a1ad4e>
"Kyle J. McKay" <mackyle@gmail.com> writes:
Show 6 quoted lines
>> OK.  So we do not expect it to fail, but we still do want the side
>> effect of that function (i.e. accmulation into the field).
>>
>> Somebody care to send a final "agreed-upon" version?
>
> Yup, here it is:
Thanks.
Show 53 quoted lines
> -- 8< --
>
> Since 6b4b013f18 (mailinfo: handle in-body header continuations,
> 2016-09-20, v2.11.0) mailinfo.c has contained new code with an
> assert of the form:
>
> 	assert(call_a_function(...))
>
> The function in question, check_header, has side effects.  This
> means that when NDEBUG is defined during a release build the
> function call is omitted entirely, the side effects do not
> take place and tests (fortunately) start failing.
>
> Move the function call outside of the assert and assert on
> the result of the function call instead so that the code
> still works properly in a release build and passes the tests.
>
> Since the only time that mi->inbody_header_accum is appended to is
> in check_inbody_header, and appending onto a blank
> mi->inbody_header_accum always happens when is_inbody_header is
> true, this guarantees a prefix that causes check_header to always
> return true.
>
> Therefore replace the assert with an if !check_header + DIE
> combination to reflect this.
>
> Helped-by: Jonathan Tan <jonathantanmy@google.com>
> Helped-by: Jeff King <peff@peff.net>
> Acked-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
> ---
>
> Notes:
>     Please include this PATCH in 2.11.x maint
>
>  mailinfo.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/mailinfo.c b/mailinfo.c
> index 2fb3877e..a489d9d0 100644
> --- a/mailinfo.c
> +++ b/mailinfo.c
> @@ -710,7 +710,8 @@ static void flush_inbody_header_accum(struct mailinfo *mi)
>  {
>  	if (!mi->inbody_header_accum.len)
>  		return;
> -	assert(check_header(mi, &mi->inbody_header_accum, mi->s_hdr_data, 0));
> +	if (!check_header(mi, &mi->inbody_header_accum, mi->s_hdr_data, 0))
> +		die("BUG: inbody_header_accum, if not empty, must always contain a valid in-body header");
>  	strbuf_reset(&mi->inbody_header_accum);
>  }
>  
> ---
Previous: Kyle J. McKayNext: Kyle J. McKay
Message 8 of 18 in “mailinfo.c: move side-effects outside of assert”
  1. mailinfo.c: move side-effects outside of assertKyle J. McKay, Dec 17, 2016
  2. Johannes SchindelinDec 19, 2016
  3. Jeff KingDec 19, 2016
  4. Kyle J. McKayDec 19, 2016
  5. Jonathan TanDec 19, 2016
  6. Junio C HamanoDec 19, 2016
  7. mailinfo.c: move side-effects outside of assertKyle J. McKay, Dec 19, 2016
  8. Junio C HamanoDec 19, 2016
  9. mailinfo.c: move side-effects outside of assertKyle J. McKay, Dec 19, 2016
  10. Johannes SchindelinDec 20, 2016
  11. Jeff KingDec 20, 2016
  12. Kyle J. McKayDec 21, 2016
  13. Jeff KingDec 21, 2016
  14. Kyle J. McKayDec 22, 2016
  15. Jeff KingDec 22, 2016
  16. Junio C HamanoDec 22, 2016
  17. Kyle J. McKayDec 22, 2016
  18. Jeff KingDec 22, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.