{"thread":{"id":"55864","subject":"[PATCH] builtins + test helpers: use return instead of exit() in cmd_*","startedAt":"2021-06-07T11:13:15Z","lastAt":"2021-06-10T13:27:42Z","messageCount":10,"participants":["Ævar Arnfjörð Bjarmason","Felipe Contreras","Jeff King","Junio C Hamano","Phillip Wood"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"426572","messageId":"patch-1.1-61d7e6e079-20210607T111008Z-avarab@gmail.com","threadId":"55864","inReplyTo":null,"subject":"[PATCH] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-06-07T11:12:48Z","receivedAt":"2021-06-07T11:13:15Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Change various cmd_* functions to use \"return\" instead of exit() to\nindicate an exit code. On Solaris with SunCC the compiler legitimately\ncomplains about these, since we'll e.g. skip the cleanup (e.g. closing\nfd's, erroring if we can't) in git.c's run_builtin() when we exit()\ndirectly like this.\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n\nA trivial issue noticed in my pre-v2.32.0 testing that I wanted to\nleave until after the release.\n\n builtin/difftool.c          | 5 ++---\n builtin/merge-ours.c        | 2 +-\n builtin/mktree.c            | 2 +-\n t/helper/test-hash-speed.c  | 2 +-\n t/helper/test-hash.c        | 2 +-\n t/helper/test-match-trees.c | 2 +-\n t/helper/test-reach.c       | 2 +-\n 7 files changed, 8 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/difftool.c b/builtin/difftool.c\nindex 89334b77fb..6a9242a803 100644\n--- a/builtin/difftool.c\n+++ b/builtin/difftool.c\n@@ -675,7 +675,7 @@ static int run_file_diff(int prompt, const char *prefix,\n \t\t\"GIT_PAGER=\", \"GIT_EXTERNAL_DIFF=git-difftool--helper\", NULL,\n \t\tNULL\n \t};\n-\tint ret = 0, i;\n+\tint i;\n \n \tif (prompt > 0)\n \t\tenv[2] = \"GIT_DIFFTOOL_PROMPT=true\";\n@@ -686,8 +686,7 @@ static int run_file_diff(int prompt, const char *prefix,\n \tstrvec_push(&args, \"diff\");\n \tfor (i = 0; i < argc; i++)\n \t\tstrvec_push(&args, argv[i]);\n-\tret = run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n-\texit(ret);\n+\treturn run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n }\n \n int cmd_difftool(int argc, const char **argv, const char *prefix)\ndiff --git a/builtin/merge-ours.c b/builtin/merge-ours.c\nindex 4594507420..a745c32ae4 100644\n--- a/builtin/merge-ours.c\n+++ b/builtin/merge-ours.c\n@@ -29,5 +29,5 @@ int cmd_merge_ours(int argc, const char **argv, const char *prefix)\n \t\tdie_errno(\"read_cache failed\");\n \tif (index_differs_from(the_repository, \"HEAD\", NULL, 0))\n \t\texit(2);\n-\texit(0);\n+\treturn 0;\n }\ndiff --git a/builtin/mktree.c b/builtin/mktree.c\nindex 891991b00d..ae78ca1c02 100644\n--- a/builtin/mktree.c\n+++ b/builtin/mktree.c\n@@ -189,5 +189,5 @@ int cmd_mktree(int ac, const char **av, const char *prefix)\n \t\tused=0; /* reset tree entry buffer for re-use in batch mode */\n \t}\n \tstrbuf_release(&sb);\n-\texit(0);\n+\treturn 0;\n }\ndiff --git a/t/helper/test-hash-speed.c b/t/helper/test-hash-speed.c\nindex 432233c7f0..f40d9ad0c2 100644\n--- a/t/helper/test-hash-speed.c\n+++ b/t/helper/test-hash-speed.c\n@@ -57,5 +57,5 @@ int cmd__hash_speed(int ac, const char **av)\n \t\tfree(p);\n \t}\n \n-\texit(0);\n+\treturn 0;\n }\ndiff --git a/t/helper/test-hash.c b/t/helper/test-hash.c\nindex 0a31de66f3..261c545b9d 100644\n--- a/t/helper/test-hash.c\n+++ b/t/helper/test-hash.c\n@@ -54,5 +54,5 @@ int cmd_hash_impl(int ac, const char **av, int algo)\n \t\tfwrite(hash, 1, algop->rawsz, stdout);\n \telse\n \t\tputs(hash_to_hex_algop(hash, algop));\n-\texit(0);\n+\treturn 0;\n }\ndiff --git a/t/helper/test-match-trees.c b/t/helper/test-match-trees.c\nindex b9fd427571..4079fdee06 100644\n--- a/t/helper/test-match-trees.c\n+++ b/t/helper/test-match-trees.c\n@@ -23,5 +23,5 @@ int cmd__match_trees(int ac, const char **av)\n \tshift_tree(the_repository, &one->object.oid, &two->object.oid, &shifted, -1);\n \tprintf(\"shifted: %s\\n\", oid_to_hex(&shifted));\n \n-\texit(0);\n+\treturn 0;\n }\ndiff --git a/t/helper/test-reach.c b/t/helper/test-reach.c\nindex cda804ed79..2f65c7f6a5 100644\n--- a/t/helper/test-reach.c\n+++ b/t/helper/test-reach.c\n@@ -166,5 +166,5 @@ int cmd__reach(int ac, const char **av)\n \t\tprint_sorted_commit_ids(list);\n \t}\n \n-\texit(0);\n+\treturn 0;\n }\n-- \n2.32.0.rc3.434.gd8aed1f08a7\n\n"},{"id":"426635","messageId":"60be5113c50a1_39c0a208e1@natae.notmuch","threadId":"55864","inReplyTo":"patch-1.1-61d7e6e079-20210607T111008Z-avarab@gmail.com","subject":"RE: [PATCH] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Felipe Contreras","fromEmail":"felipe.contreras@gmail.com","sentAt":"2021-06-07T17:02:11Z","receivedAt":"2021-06-07T17:03:33Z","isPatch":true,"sender":{"key":"felipe.contreras@gmail.com","avatar":"https://avatars.githubusercontent.com/u/8358?v=4"},"body":"Ævar Arnfjörð Bjarmason wrote:\n> Change various cmd_* functions to use \"return\" instead of exit() to\n> indicate an exit code. On Solaris with SunCC the compiler legitimately\n> complains about these, since we'll e.g. skip the cleanup (e.g. closing\n> fd's, erroring if we can't) in git.c's run_builtin() when we exit()\n> directly like this.\n> \n> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n\nMakes sense to me and it's obviously correct.\n\nReviewed-by: Felipe Contreras <felipe.contreras@gmail.com>\n\n-- \nFelipe Contreras"},{"id":"426719","messageId":"YL8S9jDmrtdKr5N+@coredump.intra.peff.net","threadId":"55864","inReplyTo":"patch-1.1-61d7e6e079-20210607T111008Z-avarab@gmail.com","subject":"Re: [PATCH] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2021-06-08T06:49:26Z","receivedAt":"2021-06-08T06:49:29Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Jun 07, 2021 at 01:12:48PM +0200, Ævar Arnfjörð Bjarmason wrote:\n\n> Change various cmd_* functions to use \"return\" instead of exit() to\n> indicate an exit code. On Solaris with SunCC the compiler legitimately\n> complains about these, since we'll e.g. skip the cleanup (e.g. closing\n> fd's, erroring if we can't) in git.c's run_builtin() when we exit()\n> directly like this.\n\nEach of these cases looks like a simple and obvious conversion, and I\ncertainly don't mind us doing it.\n\nBut I do wonder what SunCC is complaining about exactly. Calling exit()\nmeans you don't have to worry about cleanup anymore. Does the compiler\nnot have any notion of NORETURN or equivalent? If so, I'd expect many\nmore complaints in general that we probably _won't_ want to silence,\nbecause it will be awkward to do so.\n\n> diff --git a/builtin/difftool.c b/builtin/difftool.c\n> index 89334b77fb..6a9242a803 100644\n> --- a/builtin/difftool.c\n> +++ b/builtin/difftool.c\n> @@ -675,7 +675,7 @@ static int run_file_diff(int prompt, const char *prefix,\n>  \t\t\"GIT_PAGER=\", \"GIT_EXTERNAL_DIFF=git-difftool--helper\", NULL,\n>  \t\tNULL\n>  \t};\n> -\tint ret = 0, i;\n> +\tint i;\n>  \n>  \tif (prompt > 0)\n>  \t\tenv[2] = \"GIT_DIFFTOOL_PROMPT=true\";\n> @@ -686,8 +686,7 @@ static int run_file_diff(int prompt, const char *prefix,\n>  \tstrvec_push(&args, \"diff\");\n>  \tfor (i = 0; i < argc; i++)\n>  \t\tstrvec_push(&args, argv[i]);\n> -\tret = run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n> -\texit(ret);\n> +\treturn run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n>  }\n\nThis one I'm not surprised that a compiler would complain about. The\nfunction returns an int, but there are no return paths from the\nfunction (and hence the caller doing \"return run_diff_files()\" likewise\ncould not ever return there. Which is not quite what you said it\ncomplained about above, hence my curiosity. :)\n\n-Peff\n"},{"id":"426754","messageId":"patch-1.1-f225b78e01-20210608T104454Z-avarab@gmail.com","threadId":"55864","inReplyTo":"patch-1.1-61d7e6e079-20210607T111008Z-avarab@gmail.com","subject":"[PATCH v2] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-06-08T10:48:03Z","receivedAt":"2021-06-08T10:49:13Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"Change various cmd_* functions that claim no return an \"int\" to use\n\"return\" instead of exit() to indicate an exit code. These were not\nmarked with NORETURN, and by directly exit()-ing we'll skip the\ncleanup git.c would otherwise do (e.g. closing fd's, erroring if we\ncan't). See run_builtin() in git.c.\n\nIn the case of shell.c and sh-i18n--envsubst.c this was the result of\nan incomplete migration to using a cmd_main() in 3f2e2297b9 (add an\nextra level of indirection to main(), 2016-07-01).\n\nThis was spotted by SunCC 12.5 on Solaris 10 (gcc210 on the gccfarm).\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n\nClarified the commit message, and made the same s/exit/return/g change\nin shell.c and sh-i18n--envsubst.c. I also missed an \"exit(2)\" in a\nbrach in builtin/merge-ours.c.\n\nRange-diff against v1:\n1:  61d7e6e079 ! 1:  f225b78e01 builtins + test helpers: use return instead of exit() in cmd_*\n    @@ Metadata\n      ## Commit message ##\n         builtins + test helpers: use return instead of exit() in cmd_*\n     \n    -    Change various cmd_* functions to use \"return\" instead of exit() to\n    -    indicate an exit code. On Solaris with SunCC the compiler legitimately\n    -    complains about these, since we'll e.g. skip the cleanup (e.g. closing\n    -    fd's, erroring if we can't) in git.c's run_builtin() when we exit()\n    -    directly like this.\n    +    Change various cmd_* functions that claim no return an \"int\" to use\n    +    \"return\" instead of exit() to indicate an exit code. These were not\n    +    marked with NORETURN, and by directly exit()-ing we'll skip the\n    +    cleanup git.c would otherwise do (e.g. closing fd's, erroring if we\n    +    can't). See run_builtin() in git.c.\n    +\n    +    In the case of shell.c and sh-i18n--envsubst.c this was the result of\n    +    an incomplete migration to using a cmd_main() in 3f2e2297b9 (add an\n    +    extra level of indirection to main(), 2016-07-01).\n    +\n    +    This was spotted by SunCC 12.5 on Solaris 10 (gcc210 on the gccfarm).\n     \n         Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n     \n    @@ builtin/difftool.c: static int run_file_diff(int prompt, const char *prefix,\n     \n      ## builtin/merge-ours.c ##\n     @@ builtin/merge-ours.c: int cmd_merge_ours(int argc, const char **argv, const char *prefix)\n    + \tif (read_cache() < 0)\n      \t\tdie_errno(\"read_cache failed\");\n      \tif (index_differs_from(the_repository, \"HEAD\", NULL, 0))\n    - \t\texit(2);\n    +-\t\texit(2);\n     -\texit(0);\n    ++\t\treturn 2;\n     +\treturn 0;\n      }\n     \n    @@ builtin/mktree.c: int cmd_mktree(int ac, const char **av, const char *prefix)\n     +\treturn 0;\n      }\n     \n    + ## sh-i18n--envsubst.c ##\n    +@@ sh-i18n--envsubst.c: cmd_main (int argc, const char *argv[])\n    +   if (ferror (stderr) || fflush (stderr))\n    +     {\n    +       fclose (stderr);\n    +-      exit (EXIT_FAILURE);\n    ++      return (EXIT_FAILURE);\n    +     }\n    +   if (fclose (stderr) && errno != EBADF)\n    +-    exit (EXIT_FAILURE);\n    ++    return (EXIT_FAILURE);\n    + \n    +-  exit (EXIT_SUCCESS);\n    ++  return (EXIT_SUCCESS);\n    + }\n    + \n    + /* Parse the string and invoke the callback each time a $VARIABLE or\n    +\n    + ## shell.c ##\n    +@@ shell.c: int cmd_main(int argc, const char **argv)\n    + \t\tdefault:\n    + \t\t\tcontinue;\n    + \t\t}\n    +-\t\texit(cmd->exec(cmd->name, arg));\n    ++\t\treturn cmd->exec(cmd->name, arg);\n    + \t}\n    + \n    + \tcd_to_homedir();\n    +\n      ## t/helper/test-hash-speed.c ##\n     @@ t/helper/test-hash-speed.c: int cmd__hash_speed(int ac, const char **av)\n      \t\tfree(p);\n\n builtin/difftool.c          | 5 ++---\n builtin/merge-ours.c        | 4 ++--\n builtin/mktree.c            | 2 +-\n sh-i18n--envsubst.c         | 6 +++---\n shell.c                     | 2 +-\n t/helper/test-hash-speed.c  | 2 +-\n t/helper/test-hash.c        | 2 +-\n t/helper/test-match-trees.c | 2 +-\n t/helper/test-reach.c       | 2 +-\n 9 files changed, 13 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/difftool.c b/builtin/difftool.c\nindex 89334b77fb..6a9242a803 100644\n--- a/builtin/difftool.c\n+++ b/builtin/difftool.c\n@@ -675,7 +675,7 @@ static int run_file_diff(int prompt, const char *prefix,\n \t\t\"GIT_PAGER=\", \"GIT_EXTERNAL_DIFF=git-difftool--helper\", NULL,\n \t\tNULL\n \t};\n-\tint ret = 0, i;\n+\tint i;\n \n \tif (prompt > 0)\n \t\tenv[2] = \"GIT_DIFFTOOL_PROMPT=true\";\n@@ -686,8 +686,7 @@ static int run_file_diff(int prompt, const char *prefix,\n \tstrvec_push(&args, \"diff\");\n \tfor (i = 0; i < argc; i++)\n \t\tstrvec_push(&args, argv[i]);\n-\tret = run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n-\texit(ret);\n+\treturn run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n }\n \n int cmd_difftool(int argc, const char **argv, const char *prefix)\ndiff --git a/builtin/merge-ours.c b/builtin/merge-ours.c\nindex 4594507420..3583cff71c 100644\n--- a/builtin/merge-ours.c\n+++ b/builtin/merge-ours.c\n@@ -28,6 +28,6 @@ int cmd_merge_ours(int argc, const char **argv, const char *prefix)\n \tif (read_cache() < 0)\n \t\tdie_errno(\"read_cache failed\");\n \tif (index_differs_from(the_repository, \"HEAD\", NULL, 0))\n-\t\texit(2);\n-\texit(0);\n+\t\treturn 2;\n+\treturn 0;\n }\ndiff --git a/builtin/mktree.c b/builtin/mktree.c\nindex 891991b00d..ae78ca1c02 100644\n--- a/builtin/mktree.c\n+++ b/builtin/mktree.c\n@@ -189,5 +189,5 @@ int cmd_mktree(int ac, const char **av, const char *prefix)\n \t\tused=0; /* reset tree entry buffer for re-use in batch mode */\n \t}\n \tstrbuf_release(&sb);\n-\texit(0);\n+\treturn 0;\n }\ndiff --git a/sh-i18n--envsubst.c b/sh-i18n--envsubst.c\nindex e7430b9aa8..6cd307ac2c 100644\n--- a/sh-i18n--envsubst.c\n+++ b/sh-i18n--envsubst.c\n@@ -104,12 +104,12 @@ cmd_main (int argc, const char *argv[])\n   if (ferror (stderr) || fflush (stderr))\n     {\n       fclose (stderr);\n-      exit (EXIT_FAILURE);\n+      return (EXIT_FAILURE);\n     }\n   if (fclose (stderr) && errno != EBADF)\n-    exit (EXIT_FAILURE);\n+    return (EXIT_FAILURE);\n \n-  exit (EXIT_SUCCESS);\n+  return (EXIT_SUCCESS);\n }\n \n /* Parse the string and invoke the callback each time a $VARIABLE or\ndiff --git a/shell.c b/shell.c\nindex cef7ffdc9e..811e13b9c9 100644\n--- a/shell.c\n+++ b/shell.c\n@@ -177,7 +177,7 @@ int cmd_main(int argc, const char **argv)\n \t\tdefault:\n \t\t\tcontinue;\n \t\t}\n-\t\texit(cmd->exec(cmd->name, arg));\n+\t\treturn cmd->exec(cmd->name, arg);\n \t}\n \n \tcd_to_homedir();\ndiff --git a/t/helper/test-hash-speed.c b/t/helper/test-hash-speed.c\nindex 432233c7f0..f40d9ad0c2 100644\n--- a/t/helper/test-hash-speed.c\n+++ b/t/helper/test-hash-speed.c\n@@ -57,5 +57,5 @@ int cmd__hash_speed(int ac, const char **av)\n \t\tfree(p);\n \t}\n \n-\texit(0);\n+\treturn 0;\n }\ndiff --git a/t/helper/test-hash.c b/t/helper/test-hash.c\nindex 0a31de66f3..261c545b9d 100644\n--- a/t/helper/test-hash.c\n+++ b/t/helper/test-hash.c\n@@ -54,5 +54,5 @@ int cmd_hash_impl(int ac, const char **av, int algo)\n \t\tfwrite(hash, 1, algop->rawsz, stdout);\n \telse\n \t\tputs(hash_to_hex_algop(hash, algop));\n-\texit(0);\n+\treturn 0;\n }\ndiff --git a/t/helper/test-match-trees.c b/t/helper/test-match-trees.c\nindex b9fd427571..4079fdee06 100644\n--- a/t/helper/test-match-trees.c\n+++ b/t/helper/test-match-trees.c\n@@ -23,5 +23,5 @@ int cmd__match_trees(int ac, const char **av)\n \tshift_tree(the_repository, &one->object.oid, &two->object.oid, &shifted, -1);\n \tprintf(\"shifted: %s\\n\", oid_to_hex(&shifted));\n \n-\texit(0);\n+\treturn 0;\n }\ndiff --git a/t/helper/test-reach.c b/t/helper/test-reach.c\nindex cda804ed79..2f65c7f6a5 100644\n--- a/t/helper/test-reach.c\n+++ b/t/helper/test-reach.c\n@@ -166,5 +166,5 @@ int cmd__reach(int ac, const char **av)\n \t\tprint_sorted_commit_ids(list);\n \t}\n \n-\texit(0);\n+\treturn 0;\n }\n-- \n2.32.0.rc3.434.gd8aed1f08a7\n\n"},{"id":"426755","messageId":"87zgw039p1.fsf@evledraar.gmail.com","threadId":"55864","inReplyTo":"YL8S9jDmrtdKr5N+@coredump.intra.peff.net","subject":"Re: [PATCH] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-06-08T10:53:52Z","receivedAt":"2021-06-08T10:57:51Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Tue, Jun 08 2021, Jeff King wrote:\n\n> On Mon, Jun 07, 2021 at 01:12:48PM +0200, Ævar Arnfjörð Bjarmason wrote:\n>\n>> Change various cmd_* functions to use \"return\" instead of exit() to\n>> indicate an exit code. On Solaris with SunCC the compiler legitimately\n>> complains about these, since we'll e.g. skip the cleanup (e.g. closing\n>> fd's, erroring if we can't) in git.c's run_builtin() when we exit()\n>> directly like this.\n>\n> Each of these cases looks like a simple and obvious conversion, and I\n> certainly don't mind us doing it.\n>\n> But I do wonder what SunCC is complaining about exactly. Calling exit()\n> means you don't have to worry about cleanup anymore. Does the compiler\n> not have any notion of NORETURN or equivalent? If so, I'd expect many\n> more complaints in general that we probably _won't_ want to silence,\n> because it will be awkward to do so.\n\nIt does, but in this case there's no NORETURN and we declared the\ncommand to return int, and it's not the \"main\" function.\n\nI believe that's what trips it up, as noted in a v2 I just submitted\nnow:\nhttps://lore.kernel.org/git/patch-1.1-f225b78e01-20210608T104454Z-avarab@gmail.com/\n\nIt's not whining about every instance of \"exit\" in the codebase.\n\n>> diff --git a/builtin/difftool.c b/builtin/difftool.c\n>> index 89334b77fb..6a9242a803 100644\n>> --- a/builtin/difftool.c\n>> +++ b/builtin/difftool.c\n>> @@ -675,7 +675,7 @@ static int run_file_diff(int prompt, const char *prefix,\n>>  \t\t\"GIT_PAGER=\", \"GIT_EXTERNAL_DIFF=git-difftool--helper\", NULL,\n>>  \t\tNULL\n>>  \t};\n>> -\tint ret = 0, i;\n>> +\tint i;\n>>  \n>>  \tif (prompt > 0)\n>>  \t\tenv[2] = \"GIT_DIFFTOOL_PROMPT=true\";\n>> @@ -686,8 +686,7 @@ static int run_file_diff(int prompt, const char *prefix,\n>>  \tstrvec_push(&args, \"diff\");\n>>  \tfor (i = 0; i < argc; i++)\n>>  \t\tstrvec_push(&args, argv[i]);\n>> -\tret = run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n>> -\texit(ret);\n>> +\treturn run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n>>  }\n>\n> This one I'm not surprised that a compiler would complain about. The\n> function returns an int, but there are no return paths from the\n> function (and hence the caller doing \"return run_diff_files()\" likewise\n> could not ever return there. Which is not quite what you said it\n> complained about above, hence my curiosity. :)\n\nI sent a few miscellaneous fixes recently for 15-20 SunCC\nwarnings. Around 1/2 of these are legitimate issues like these, the rest\nare bugs in SunCC or flaws in it flow analysis or other known bugs in\nthe compiler.\n"},{"id":"426826","messageId":"xmqqwnr3nc7i.fsf@gitster.g","threadId":"55864","inReplyTo":"patch-1.1-f225b78e01-20210608T104454Z-avarab@gmail.com","subject":"Re: [PATCH v2] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-06-08T23:55:29Z","receivedAt":"2021-06-08T23:55:33Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:\n\n> Change various cmd_* functions that claim no return an \"int\" to use\n\ns/no return/to return/\n\n> \"return\" instead of exit() to indicate an exit code. These were not\n> marked with NORETURN,\n\nUp to this point, it is well written.\n\n> and by directly exit()-ing we'll skip the\n> cleanup git.c would otherwise do (e.g. closing fd's, erroring if we\n> can't). See run_builtin() in git.c.\n\nBut I think this is a hyperbole.  File descritors are closed when we\nexit without git.c's help, thank-you-very-much ;-), and if we do\nhave clean-ups that are truly important, we would have arranged them\nto happen in the atexit handler, so it is not a crime for functions\ncalled from the subcommand dispatchers to exit themselves (as long\nas they exit sensibly, e.g. without doing nonsense like exit(-1)).\n\nIt nevertheless is a good idea because it encourages good code\nhygiene, just like marking with NORETURN if the function must exit.\nSelling this change as if it were a correctness fix (i.e. we were\nexiting and missed these important clean-ups that the caller wanted\nto do after we return) is misleading.\n\n> In the case of shell.c and sh-i18n--envsubst.c this was the result of\n> an incomplete migration to using a cmd_main() in 3f2e2297b9 (add an\n> extra level of indirection to main(), 2016-07-01).\n>\n> This was spotted by SunCC 12.5 on Solaris 10 (gcc210 on the gccfarm).\n>\n> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n> ---\n>\n> Clarified the commit message, and made the same s/exit/return/g change\n> in shell.c and sh-i18n--envsubst.c. I also missed an \"exit(2)\" in a\n> brach in builtin/merge-ours.c.\n\nThe range diff looks good to me.  Thanks.\n"},{"id":"426834","messageId":"87im2n3gje.fsf@evledraar.gmail.com","threadId":"55864","inReplyTo":"xmqqwnr3nc7i.fsf@gitster.g","subject":"Re: [PATCH v2] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-06-09T01:54:22Z","receivedAt":"2021-06-09T02:42:18Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Wed, Jun 09 2021, Junio C Hamano wrote:\n\n> Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:\n>\n>> Change various cmd_* functions that claim no return an \"int\" to use\n>\n> s/no return/to return/\n>\n>> \"return\" instead of exit() to indicate an exit code. These were not\n>> marked with NORETURN,\n>\n> Up to this point, it is well written.\n>\n>> and by directly exit()-ing we'll skip the\n>> cleanup git.c would otherwise do (e.g. closing fd's, erroring if we\n>> can't). See run_builtin() in git.c.\n>\n> But I think this is a hyperbole.  File descritors are closed when we\n> exit without git.c's help, thank-you-very-much ;-), [...]\n\nClosed yes, but not \", erroring if we can't\". That's referring to the\nbehavior in git.c added in g0f157315a1 (Check for IO errors after\nrunning a command, 2007-06-24) and 0227f9887b (git: Try a bit harder not\nto lose errno in stdio, 2007-06-30).\n\nThat strictness isn't something you get by default from an exiting C\nprogram, which is why we're explicitly checking and calling die_errno()\nin run_builtin().\n\nI wasn't aiming for hyperbole, just accurately describing the\nimplications of skipping the code we'd skip before this patch.\n\n> [...]and if we do\n> have clean-ups that are truly important, we would have arranged them\n> to happen in the atexit handler, so it is not a crime for functions\n> called from the subcommand dispatchers to exit themselves (as long\n> as they exit sensibly, e.g. without doing nonsense like exit(-1)).\n\nI'm not quite sure what \"clean-ups that are truly important\" is meant to\nget at here. I was just describing the cleanups in git.c that we were\nskipping, which aren't implemened as atexit handlers.\n\nBut no, those couldn't be done in atexit handlers as they call\ndie_errno() or BUG(), and both of them want to modify the exit code. The\natexit() handlers cannot modify the exit code (both per the C standard,\nand POSIX).\n\nThat particular edge was last last discussed on-list in my\nhttps://lore.kernel.org/git/20210202020001.31601-6-avarab@gmail.com/;\nwhen the whole \"should SIGPIPE from the pager be ignored\" topic came up.\n\nSo it's really the opposite of what you're saying. If you have cleanups\nthat are truly important, i.e. so important that you'd like to notify\nthe user with a non-zero exit code if they fail, you *don't* want them\nin an atexit handler. That won't work.\n\n> It nevertheless is a good idea because it encourages good code\n> hygiene, just like marking with NORETURN if the function must exit.\n> Selling this change as if it were a correctness fix (i.e. we were\n> exiting and missed these important clean-ups that the caller wanted\n> to do after we return) is misleading.\n\nBefore this patch:\n\n    $ git ls-tree HEAD | git mktree >/dev/full; echo $?\n    0\n\nAfter:\n\n    $ git ls-tree HEAD | git mktree >/dev/full; echo $?\n    fatal: unknown write failure on standard output\n    128\n\nSo yes, it's a correctness fix, and you can't do that in an atexit\nhandler, at least not portably.\n\nYou might find that if you try it that it works perfectly fine. But\nthat's because e.g. glibc does non-standard shenanigans to make it work,\nbut it's not portable behavior. See\ne.g. https://wiki.musl-libc.org/functional-differences-from-glibc.html#Re_entrancy_of_exit\n\nThat page suggests that glibc's behavior might be an accident, but it's\nnot. They explicitly support that non-standard behavior of an atexit\nhandler munging the exit code. See their implementation & comments:\nhttps://github.com/bminor/glibc/blob/master/stdlib/exit.c\n"},{"id":"426835","messageId":"xmqqy2bjlnbw.fsf@gitster.g","threadId":"55864","inReplyTo":"87im2n3gje.fsf@evledraar.gmail.com","subject":"Re: [PATCH v2] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-06-09T03:38:11Z","receivedAt":"2021-06-09T03:38:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:\n\n> So it's really the opposite of what you're saying. If you have cleanups\n> that are truly important, i.e. so important that you'd like to notify\n> the user with a non-zero exit code if they fail, you *don't* want them\n> in an atexit handler. That won't work.\n\nAh, OK.  What I had in mind was things like removing the directory\n\"clone\" attempted to create and populate, removing temporary files,\netc.  when a function that is not marked as NORETURN calls die(), by\nthe atexit handler.  But you're right.  We leave a final clean-up\nfor normal returns (i.e. when cmd_foo() intends to return or exit\nwith 0) to be done to the caller that is git.::run_builtin().\n\nThanks.\n"},{"id":"426999","messageId":"71775f7d-eb60-7fdc-731e-d370f620735e@gmail.com","threadId":"55864","inReplyTo":"YL8S9jDmrtdKr5N+@coredump.intra.peff.net","subject":"Re: [PATCH] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2021-06-10T13:16:37Z","receivedAt":"2021-06-10T13:16:42Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 08/06/2021 07:49, Jeff King wrote:\n> On Mon, Jun 07, 2021 at 01:12:48PM +0200, Ævar Arnfjörð Bjarmason wrote:\n> \n>> Change various cmd_* functions to use \"return\" instead of exit() to\n>> indicate an exit code. On Solaris with SunCC the compiler legitimately\n>> complains about these, since we'll e.g. skip the cleanup (e.g. closing\n>> fd's, erroring if we can't) in git.c's run_builtin() when we exit()\n>> directly like this.\n> \n> Each of these cases looks like a simple and obvious conversion, and I\n> certainly don't mind us doing it.\n> \n> But I do wonder what SunCC is complaining about exactly. Calling exit()\n> means you don't have to worry about cleanup anymore. Does the compiler\n> not have any notion of NORETURN or equivalent? If so, I'd expect many\n> more complaints in general that we probably _won't_ want to silence,\n> because it will be awkward to do so.\n\nIt is curious that is only complaining abut exit() calls and not die(), \nmaybe that is just a coincidence though if it is not complaining about \nall calls to exit()\n\nBest Wishes\n\nPhillip\n\n>> diff --git a/builtin/difftool.c b/builtin/difftool.c\n>> index 89334b77fb..6a9242a803 100644\n>> --- a/builtin/difftool.c\n>> +++ b/builtin/difftool.c\n>> @@ -675,7 +675,7 @@ static int run_file_diff(int prompt, const char *prefix,\n>>   \t\t\"GIT_PAGER=\", \"GIT_EXTERNAL_DIFF=git-difftool--helper\", NULL,\n>>   \t\tNULL\n>>   \t};\n>> -\tint ret = 0, i;\n>> +\tint i;\n>>   \n>>   \tif (prompt > 0)\n>>   \t\tenv[2] = \"GIT_DIFFTOOL_PROMPT=true\";\n>> @@ -686,8 +686,7 @@ static int run_file_diff(int prompt, const char *prefix,\n>>   \tstrvec_push(&args, \"diff\");\n>>   \tfor (i = 0; i < argc; i++)\n>>   \t\tstrvec_push(&args, argv[i]);\n>> -\tret = run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n>> -\texit(ret);\n>> +\treturn run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n>>   }\n> \n> This one I'm not surprised that a compiler would complain about. The\n> function returns an int, but there are no return paths from the\n> function (and hence the caller doing \"return run_diff_files()\" likewise\n> could not ever return there. Which is not quite what you said it\n> complained about above, hence my curiosity. :)\n> \n> -Peff\n> \n"},{"id":"427003","messageId":"87v96l26k8.fsf@evledraar.gmail.com","threadId":"55864","inReplyTo":"71775f7d-eb60-7fdc-731e-d370f620735e@gmail.com","subject":"Re: [PATCH] builtins + test helpers: use return instead of exit() in cmd_*","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-06-10T13:19:37Z","receivedAt":"2021-06-10T13:27:42Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Thu, Jun 10 2021, Phillip Wood wrote:\n\n> On 08/06/2021 07:49, Jeff King wrote:\n>> On Mon, Jun 07, 2021 at 01:12:48PM +0200, Ævar Arnfjörð Bjarmason wrote:\n>> \n>>> Change various cmd_* functions to use \"return\" instead of exit() to\n>>> indicate an exit code. On Solaris with SunCC the compiler legitimately\n>>> complains about these, since we'll e.g. skip the cleanup (e.g. closing\n>>> fd's, erroring if we can't) in git.c's run_builtin() when we exit()\n>>> directly like this.\n>> Each of these cases looks like a simple and obvious conversion, and\n>> I\n>> certainly don't mind us doing it.\n>> But I do wonder what SunCC is complaining about exactly. Calling\n>> exit()\n>> means you don't have to worry about cleanup anymore. Does the compiler\n>> not have any notion of NORETURN or equivalent? If so, I'd expect many\n>> more complaints in general that we probably _won't_ want to silence,\n>> because it will be awkward to do so.\n>\n> It is curious that is only complaining abut exit() calls and not\n> die(), maybe that is just a coincidence though if it is not\n> complaining about all calls to exit()\n\nIt's \"function has no return statement\", usually things that die() will\nalso return if nothing goes wrong.\n\nIt also complains about e.g. cram() in imap-send.c under NO_OPENSSL=Y,\nwhich is an \"non-void\" returning function stub that just calls die().\n\nI recall raising that on-list in the past and proposing that they have a\ndummy return value, but that met lukewarm support, so I just consider\nthem false alarms and try to ignore them.\n\nI do think we should have \"die_report\" or whatever versions of die() so you could do:\n\n    return die_report(...);\n\nYou can do that with error(), but that means changing the error message\nfrom \"fatal:\" to \"error:\". Those cases are obscure though, e.g. piping\nto a full disk where we'd die for other reasons. Now we'll hide that\nerror.\n\nI mean, we'd do so anyway since cmd_builtin() in git.c won't reach the\n\"closing fd's\" check, but that could be fixed, and there's other similar\ncases where we needlessly conflate the desire to say \"fatal\" and exit\nwith 128 with returning an error and doing cleanup etc.\n\n>>> diff --git a/builtin/difftool.c b/builtin/difftool.c\n>>> index 89334b77fb..6a9242a803 100644\n>>> --- a/builtin/difftool.c\n>>> +++ b/builtin/difftool.c\n>>> @@ -675,7 +675,7 @@ static int run_file_diff(int prompt, const char *prefix,\n>>>   \t\t\"GIT_PAGER=\", \"GIT_EXTERNAL_DIFF=git-difftool--helper\", NULL,\n>>>   \t\tNULL\n>>>   \t};\n>>> -\tint ret = 0, i;\n>>> +\tint i;\n>>>     \tif (prompt > 0)\n>>>   \t\tenv[2] = \"GIT_DIFFTOOL_PROMPT=true\";\n>>> @@ -686,8 +686,7 @@ static int run_file_diff(int prompt, const char *prefix,\n>>>   \tstrvec_push(&args, \"diff\");\n>>>   \tfor (i = 0; i < argc; i++)\n>>>   \t\tstrvec_push(&args, argv[i]);\n>>> -\tret = run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n>>> -\texit(ret);\n>>> +\treturn run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);\n>>>   }\n>> This one I'm not surprised that a compiler would complain about. The\n>> function returns an int, but there are no return paths from the\n>> function (and hence the caller doing \"return run_diff_files()\" likewise\n>> could not ever return there. Which is not quite what you said it\n>> complained about above, hence my curiosity. :)\n>> -Peff\n>> \n\n"}]}