aboutsummaryrefslogtreecommitdiffstats
path: root/common-main.c
diff options
context:
space:
mode:
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>2022-06-02 14:25:33 +0200
committerJunio C Hamano <gitster@pobox.com>2022-06-02 12:51:35 -0700
commit0cc05b044fd690f37565262a5b09f60c203c5218 (patch)
tree30682a4f70be5dc2665554dd0f840d6df4ba0e99 /common-main.c
parent19d75948ef7abe9066efa4462108827d70565ae9 (diff)
downloadgit-0cc05b044fd690f37565262a5b09f60c203c5218.tar.gz
usage.c: add a non-fatal bug() function to go with BUG()
Add a bug() function to use in cases where we'd like to indicate a runtime BUG(), but would like to defer the BUG() call because we're possibly accumulating more bug() callers to exhaustively indicate what went wrong. We already have this sort of facility in various parts of the codebase, just in the form of ad-hoc re-inventions of the functionality that this new API provides. E.g. this will be used to replace optbug() in parse-options.c, and the 'error("BUG:[...]' we do in a loop in builtin/receive-pack.c. Unlike the code this replaces we'll log to trace2 with this new bug() function (as with other usage.c functions, including BUG()), we'll also be able to avoid calls to xstrfmt() in some cases, as the bug() function itself accepts variadic sprintf()-like arguments. Any caller to bug() can follow up such calls with BUG_if_bug(), which will BUG() out (i.e. abort()) if there were any preceding calls to bug(), callers can also decide not to call BUG_if_bug() and leave the resulting BUG() invocation until exit() time. There are currently no bug() API users that don't call BUG_if_bug() themselves after a for-loop, but allowing for not calling BUG_if_bug() keeps the API flexible. As the tests and documentation here show we'll catch missing BUG_if_bug() invocations in our exit() wrapper. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'common-main.c')
-rw-r--r--common-main.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/common-main.c b/common-main.c
index b6124dd2c2..c531372f3f 100644
--- a/common-main.c
+++ b/common-main.c
@@ -59,6 +59,13 @@ int main(int argc, const char **argv)
exit(result);
}
+static void check_bug_if_BUG(void)
+{
+ if (!bug_called_must_BUG)
+ return;
+ BUG("on exit(): had bug() call(s) in this process without explicit BUG_if_bug()");
+}
+
/* We wrap exit() to call common_exit() in git-compat-util.h */
int common_exit(const char *file, int line, int code)
{
@@ -70,6 +77,7 @@ int common_exit(const char *file, int line, int code)
*/
code &= 0xff;
+ check_bug_if_BUG();
trace2_cmd_exit_fl(file, line, code);
return code;