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

From: Ævar Arnfjörð Bjarmason <[email-masked]>
Date: Thu, 10 Jun 2021 15:19:37 +0200

On Thu, Jun 10 2021, Phillip Wood wrote:

> On 08/06/2021 07:49, Jeff King wrote:
>> On Mon, Jun 07, 2021 at 01:12:48PM +0200, Ævar Arnfjörð Bjarmason wrote:
>> 
>>> Change various cmd_* functions to use "return" instead of exit() to
>>> indicate an exit code. On Solaris with SunCC the compiler legitimately
>>> complains about these, since we'll e.g. skip the cleanup (e.g. closing
>>> fd's, erroring if we can't) in git.c's run_builtin() when we exit()
>>> directly like this.
>> Each of these cases looks like a simple and obvious conversion, and
>> I
>> certainly don't mind us doing it.
>> But I do wonder what SunCC is complaining about exactly. Calling
>> exit()
>> means you don't have to worry about cleanup anymore. Does the compiler
>> not have any notion of NORETURN or equivalent? If so, I'd expect many
>> more complaints in general that we probably _won't_ want to silence,
>> because it will be awkward to do so.
>
> It is curious that is only complaining abut exit() calls and not
> die(), maybe that is just a coincidence though if it is not
> complaining about all calls to exit()

It's "function has no return statement", usually things that die() will
also return if nothing goes wrong.

It also complains about e.g. cram() in imap-send.c under NO_OPENSSL=Y,
which is an "non-void" returning function stub that just calls die().

I recall raising that on-list in the past and proposing that they have a
dummy return value, but that met lukewarm support, so I just consider
them false alarms and try to ignore them.

I do think we should have "die_report" or whatever versions of die() so you could do:

    return die_report(...);

You can do that with error(), but that means changing the error message
from "fatal:" to "error:". Those cases are obscure though, e.g. piping
to a full disk where we'd die for other reasons. Now we'll hide that
error.

I mean, we'd do so anyway since cmd_builtin() in git.c won't reach the
"closing fd's" check, but that could be fixed, and there's other similar
cases where we needlessly conflate the desire to say "fatal" and exit
with 128 with returning an error and doing cleanup etc.

>>> diff --git a/builtin/difftool.c b/builtin/difftool.c
>>> index 89334b77fb..6a9242a803 100644
>>> --- a/builtin/difftool.c
>>> +++ b/builtin/difftool.c
>>> @@ -675,7 +675,7 @@ static int run_file_diff(int prompt, const char *prefix,
>>>   		"GIT_PAGER=", "GIT_EXTERNAL_DIFF=git-difftool--helper", NULL,
>>>   		NULL
>>>   	};
>>> -	int ret = 0, i;
>>> +	int i;
>>>     	if (prompt > 0)
>>>   		env[2] = "GIT_DIFFTOOL_PROMPT=true";
>>> @@ -686,8 +686,7 @@ static int run_file_diff(int prompt, const char *prefix,
>>>   	strvec_push(&args, "diff");
>>>   	for (i = 0; i < argc; i++)
>>>   		strvec_push(&args, argv[i]);
>>> -	ret = run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);
>>> -	exit(ret);
>>> +	return run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);
>>>   }
>> This one I'm not surprised that a compiler would complain about. The
>> function returns an int, but there are no return paths from the
>> function (and hence the caller doing "return run_diff_files()" likewise
>> could not ever return there. Which is not quite what you said it
>> complained about above, hence my curiosity. :)
>> -Peff
>>