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

Re: [PATCH v2] builtins + test helpers: use return instead of exit() in cmd_*

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 8, 2021, 23:55 UTC
Message-ID
<xmqqwnr3nc7i.fsf@gitster.g>
In-Reply-To
<patch-1.1-f225b78e01-20210608T104454Z-avarab@gmail.com>
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
> Change various cmd_* functions that claim no return an "int" to use
s/no return/to return/
> "return" instead of exit() to indicate an exit code. These were not
> marked with NORETURN,
Up to this point, it is well written.
> and by directly exit()-ing we'll skip the
> cleanup git.c would otherwise do (e.g. closing fd's, erroring if we
> can't). See run_builtin() in git.c.

But I think this is a hyperbole. File descritors are closed when we exit without git.c's help, thank-you-very-much ;-), and if we do have clean-ups that are truly important, we would have arranged them to happen in the atexit handler, so it is not a crime for functions called from the subcommand dispatchers to exit themselves (as long as they exit sensibly, e.g. without doing nonsense like exit(-1)).

It nevertheless is a good idea because it encourages good code hygiene, just like marking with NORETURN if the function must exit. Selling this change as if it were a correctness fix (i.e. we were exiting and missed these important clean-ups that the caller wanted to do after we return) is misleading.

Show 12 quoted lines
> In the case of shell.c and sh-i18n--envsubst.c this was the result of
> an incomplete migration to using a cmd_main() in 3f2e2297b9 (add an
> extra level of indirection to main(), 2016-07-01).
>
> This was spotted by SunCC 12.5 on Solaris 10 (gcc210 on the gccfarm).
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>
> Clarified the commit message, and made the same s/exit/return/g change
> in shell.c and sh-i18n--envsubst.c. I also missed an "exit(2)" in a
> brach in builtin/merge-ours.c.
The range diff looks good to me.  Thanks.
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 8 of 10 in “builtins + test helpers: use return instead of exit() in cmd_*”
  1. builtins + test helpers: use return instead of exit() in cmd_*Ævar Arnfjörð Bjarmason, Jun 7, 2021
  2. Felipe ContrerasJun 7, 2021
  3. Jeff KingJun 8, 2021
  4. Ævar Arnfjörð BjarmasonJun 8, 2021
  5. Phillip WoodJun 10, 2021
  6. Ævar Arnfjörð BjarmasonJun 10, 2021
  7. builtins + test helpers: use return instead of exit() in cmd_*Ævar Arnfjörð Bjarmason, Jun 8, 2021
  8. Junio C HamanoJun 8, 2021
  9. Ævar Arnfjörð BjarmasonJun 9, 2021
  10. Junio C HamanoJun 9, 2021

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.