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

Re: [PATCH 1/5] usage.c: add a non-fatal bug() function to go with BUG()

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
May 26, 2022, 07:59 UTC
Message-ID
<220526.86fskw7mbv.gmgdl@evledraar.gmail.com>
In-Reply-To
<xmqqh75d2aif.fsf@gitster.g>
On Wed, May 25 2022, Junio C Hamano wrote:
Show 11 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>>> +/* usage.c: if bug() is called we should have a BUG_if_bug() afterwards */
>>> +extern int bug_called_must_BUG;
>>
>> I am not sure about the name, ...
>
> I finally figured out why I found this name so disturbing.  This is
> written from the viewpoint of somebody who is trying to catch a
> programmer's error of calling bug() without calling BUG_if_bug();
> it is not named to help the users of API to understand it better.

I named it like that to indicate a "lesser bug", I think BUG_if_bug() came later, because ...

Show 21 quoted lines
> I wonder if it makes sense to make the call to BUG_if_bug() totally
> optional.  The rule becomes slightly different:
>
>  * You can call bug() zero or more times.  It issues a fatal error
>    message, and internally remembers the fact that we detected a
>    programming error that invalidates the execution of this program,
>    without immediately terminating the program.
>
>  * When you exit() from the program, the runtime consults that "did
>    we call bug()?" record and makes the program exit with known exit
>    code (we could arrange it to abort() just like BUG, but I'd
>    prefer controlled crash).
>
>  * But it is inconvenient to always keep going, after you may have
>    called one or more bug(), to the normal program completion.  So
>    there is a helper exit_if_called_bug(), which is an equivalent to
>    checking the "did we call bug()?" record and calling exit().
>
> By making BUG_if_bug() optional, we can lose a handful of lines from
> the test helper, because it makes it a non-issue to exit the program
> without calling it.

I don't think we should do it like that and keep it a BUG() not to call BUG_if_bug() when we hit exit(), because e.g. in the case of parse-options.c once we have the a bad "struct options" we don't want to continue, as we might segfault, or have other bad behavior etc. So we'd like to BUG() out as soon as possible.

That's how we use BUG() itself, i.e. we think the program execution is bad and we immediately abort(), the new bug() makes a small concession that we may be OK for a little while (e.g. while looping over the options), but would like to BUG() out immediately after that.

Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 49 in “usage API: add and use a bug() + BUG_if_bug()”
  1. 0/5 usage API: add and use a bug() + BUG_if_bug()Ævar Arnfjörð Bjarmason, May 21, 2022
  2. 1/5 usage.c: add a non-fatal bug() function to go with BUG()Ævar Arnfjörð Bjarmason, May 21, 2022
  3. Junio C HamanoMay 25, 2022
  4. Junio C HamanoMay 26, 2022
  5. Ævar Arnfjörð BjarmasonMay 26, 2022
  6. Junio C HamanoMay 26, 2022
  7. Ævar Arnfjörð BjarmasonMay 26, 2022
  8. Junio C HamanoMay 26, 2022
  9. Ævar Arnfjörð BjarmasonMay 31, 2022
  10. Josh SteadmonMay 27, 2022
  11. Junio C HamanoMay 27, 2022
  12. 2/5 parse-options.c: use new bug() API for optbug()Ævar Arnfjörð Bjarmason, May 21, 2022
  13. Josh SteadmonMay 27, 2022
  14. 3/5 parse-options.c: use optbug() instead of BUG() "opts" checkÆvar Arnfjörð Bjarmason, May 21, 2022
  15. Junio C HamanoMay 25, 2022
  16. 4/5 receive-pack: use bug() and BUG_if_bug()Ævar Arnfjörð Bjarmason, May 21, 2022
  17. Junio C HamanoMay 25, 2022
  18. Josh SteadmonMay 27, 2022
  19. Andrei RybakMay 27, 2022
  20. 5/5 cache-tree.c: use bug() and BUG_if_bug()Ævar Arnfjörð Bjarmason, May 21, 2022
  21. Glen ChooMay 27, 2022
  22. Josh SteadmonMay 27, 2022
  23. 0/6 usage API: add and use a bug() + BUG_if_bug()Ævar Arnfjörð Bjarmason, May 31, 2022
  24. 1/6 common-main.o: move non-trace2 exit() behavior out of trace2.cÆvar Arnfjörð Bjarmason, May 31, 2022
  25. Junio C HamanoJun 1, 2022
  26. 2/6 usage.c: add a non-fatal bug() function to go with BUG()Ævar Arnfjörð Bjarmason, May 31, 2022
  27. Glen ChooMay 31, 2022
  28. Junio C HamanoJun 1, 2022
  29. 3/6 parse-options.c: use new bug() API for optbug()Ævar Arnfjörð Bjarmason, May 31, 2022
  30. 4/6 parse-options.c: use optbug() instead of BUG() "opts" checkÆvar Arnfjörð Bjarmason, May 31, 2022
  31. Josh SteadmonMay 31, 2022
  32. Junio C HamanoJun 1, 2022
  33. 5/6 receive-pack: use bug() and BUG_if_bug()Ævar Arnfjörð Bjarmason, May 31, 2022
  34. 6/6 cache-tree.c: use bug() and BUG_if_bug()Ævar Arnfjörð Bjarmason, May 31, 2022
  35. Junio C HamanoJun 1, 2022
  36. Josh SteadmonMay 31, 2022
  37. 0/6 usage API: add and use a bug() + BUG_if_bug()Ævar Arnfjörð Bjarmason, Jun 2, 2022
  38. 1/6 common-main.o: move non-trace2 exit() behavior out of trace2.cÆvar Arnfjörð Bjarmason, Jun 2, 2022
  39. Junio C HamanoJun 3, 2022
  40. Josh SteadmonJun 7, 2022
  41. Ævar Arnfjörð BjarmasonJun 7, 2022
  42. Josh SteadmonJun 7, 2022
  43. 3/6 parse-options.c: use new bug() API for optbug()Ævar Arnfjörð Bjarmason, Jun 2, 2022
  44. 2/6 usage.c: add a non-fatal bug() function to go with BUG()Ævar Arnfjörð Bjarmason, Jun 2, 2022
  45. 5/6 receive-pack: use bug() and BUG_if_bug()Ævar Arnfjörð Bjarmason, Jun 2, 2022
  46. 4/6 parse-options.c: use optbug() instead of BUG() "opts" checkÆvar Arnfjörð Bjarmason, Jun 2, 2022
  47. 6/6 cache-tree.c: use bug() and BUG_if_bug()Ævar Arnfjörð Bjarmason, Jun 2, 2022
  48. Junio C HamanoJun 2, 2022
  49. Glen ChooJun 2, 2022

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.