builtins + test helpers: use return instead of exit() in cmd_*

Change various cmd_* functions that claim to return an "int" to use "return" instead of exit() to indicate an exit code. These were not marked with NORETURN, 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. 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jun 8, 2021 at 12:48 UTC 338abb0f045b87df5e628543800e74dec0e72cdf
9 files changed +13 -14
builtin/difftool.c
+2 -3
@@ -671,7 +671,7 @@ static int run_file_diff(int prompt, const char *prefix,
671 "GIT_PAGER=", "GIT_EXTERNAL_DIFF=git-difftool--helper", NULL,
672 NULL
673 };
674 - int ret = 0, i;
674 + int i;
675
676 if (prompt > 0)
677 env[2] = "GIT_DIFFTOOL_PROMPT=true";
@@ -682,8 +682,7 @@ static int run_file_diff(int prompt, const char *prefix,
682 strvec_push(&args, "diff");
683 for (i = 0; i < argc; i++)
684 strvec_push(&args, argv[i]);
685 - ret = run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);
686 - exit(ret);
685 + return run_command_v_opt_cd_env(args.v, RUN_GIT_CMD, prefix, env);
686 }
687
688 int cmd_difftool(int argc, const char **argv, const char *prefix)
builtin/merge-ours.c
+2 -2
@@ -28,6 +28,6 @@ int cmd_merge_ours(int argc, const char **argv, const char *prefix)
28 if (read_cache() < 0)
29 die_errno("read_cache failed");
30 if (index_differs_from(the_repository, "HEAD", NULL, 0))
31 - exit(2);
32 - exit(0);
31 + return 2;
32 + return 0;
33 }
builtin/mktree.c
+1 -1
@@ -189,5 +189,5 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
189 used=0; /* reset tree entry buffer for re-use in batch mode */
190 }
191 strbuf_release(&sb);
192 - exit(0);
192 + return 0;
193 }
sh-i18n--envsubst.c
+3 -3
@@ -104,12 +104,12 @@ cmd_main (int argc, const char *argv[])
104 if (ferror (stderr) || fflush (stderr))
105 {
106 fclose (stderr);
107 - exit (EXIT_FAILURE);
107 + return (EXIT_FAILURE);
108 }
109 if (fclose (stderr) && errno != EBADF)
110 - exit (EXIT_FAILURE);
110 + return (EXIT_FAILURE);
111
112 - exit (EXIT_SUCCESS);
112 + return (EXIT_SUCCESS);
113 }
114
115 /* Parse the string and invoke the callback each time a $VARIABLE or
shell.c
+1 -1
@@ -177,7 +177,7 @@ int cmd_main(int argc, const char **argv)
177 default:
178 continue;
179 }
180 - exit(cmd->exec(cmd->name, arg));
180 + return cmd->exec(cmd->name, arg);
181 }
182
183 cd_to_homedir();
t/helper/test-hash-speed.c
+1 -1
@@ -57,5 +57,5 @@ int cmd__hash_speed(int ac, const char **av)
57 free(p);
58 }
59
60 - exit(0);
60 + return 0;
61 }
t/helper/test-hash.c
+1 -1
@@ -54,5 +54,5 @@ int cmd_hash_impl(int ac, const char **av, int algo)
54 fwrite(hash, 1, algop->rawsz, stdout);
55 else
56 puts(hash_to_hex_algop(hash, algop));
57 - exit(0);
57 + return 0;
58 }
t/helper/test-match-trees.c
+1 -1
@@ -23,5 +23,5 @@ int cmd__match_trees(int ac, const char **av)
23 shift_tree(the_repository, &one->object.oid, &two->object.oid, &shifted, -1);
24 printf("shifted: %s\n", oid_to_hex(&shifted));
25
26 - exit(0);
26 + return 0;
27 }
t/helper/test-reach.c
+1 -1
@@ -166,5 +166,5 @@ int cmd__reach(int ac, const char **av)
166 print_sorted_commit_ids(list);
167 }
168
169 - exit(0);
169 + return 0;
170 }