usage.c: add BUG() function
authorJeff King <peff@peff.net>
Sat, 13 May 2017 03:28:50 +0000 (23:28 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 15 May 2017 02:29:51 +0000 (11:29 +0900)
There's a convention in Git's code base to write assertions
as:

if (...some_bad_thing...)
die("BUG: the terrible thing happened");

with the idea that users should never see a "BUG:" message
(but if they, it at least gives a clue what happened). We
use die() here because it's convenient, but there are a few
draw-backs:

1. Without parsing the messages, it's hard for callers to
distinguish BUG assertions from regular errors.

For instance, it would be nice if the test suite could
check that we don't hit any assertions, but
test_must_fail will pass BUG deaths as OK.

2. It would be useful to add more debugging features to
BUG assertions, like file/line numbers or dumping core.

3. The die() handler can be replaced, and might not
actually exit the whole program (e.g., it may just
pthread_exit()). This is convenient for normal errors,
but for an assertion failure (which is supposed to
never happen), we're probably better off taking down
the whole process as quickly and cleanly as possible.

We could address these by checking in die() whether the
error message starts with "BUG", and behaving appropriately.
But there's little advantage at that point to sharing the
die() code, and only downsides (e.g., we can't change the
BUG() interface independently). Moreover, converting all of
the existing BUG calls reveals that the test suite does
indeed trigger a few of them.

Instead, this patch introduces a new BUG() function, which
prints an error before dying via SIGABRT. This gives us test
suite checking and core dumps. The function is actually a
macro (when supported) so that we can show the file/line
number.

We can convert die("BUG") invocations to BUG() in further
patches, dealing with any test fallouts individually.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-compat-util.h
usage.c
index bd04564a69a052ce856f2ba120855541c66fc650..4575b3890b22599d147211576d2f1ea6e7c6833e 100644 (file)
@@ -1064,6 +1064,15 @@ static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
 #define HAVE_VARIADIC_MACROS 1
 #endif
 
+#ifdef HAVE_VARIADIC_MACROS
+__attribute__((format (printf, 3, 4))) NORETURN
+void BUG_fl(const char *file, int line, const char *fmt, ...);
+#define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
+#else
+__attribute__((format (printf, 1, 2))) NORETURN
+void BUG(const char *fmt, ...);
+#endif
+
 /*
  * Preserves errno, prints a message, but gives no warning for ENOENT.
  * Returns 0 on success, which includes trying to unlink an object that does
diff --git a/usage.c b/usage.c
index ad6d2910fb58e2f27a52646bd79a0442e689517f..7e6cb2028010d0b1c04bd1423a4de32fc759a4bd 100644 (file)
--- a/usage.c
+++ b/usage.c
@@ -201,3 +201,35 @@ void warning(const char *warn, ...)
        warn_routine(warn, params);
        va_end(params);
 }
+
+static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
+{
+       char prefix[256];
+
+       /* truncation via snprintf is OK here */
+       if (file)
+               snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
+       else
+               snprintf(prefix, sizeof(prefix), "BUG: ");
+
+       vreportf(prefix, fmt, params);
+       abort();
+}
+
+#ifdef HAVE_VARIADIC_MACROS
+void BUG_fl(const char *file, int line, const char *fmt, ...)
+{
+       va_list ap;
+       va_start(ap, fmt);
+       BUG_vfl(file, line, fmt, ap);
+       va_end(ap);
+}
+#else
+void BUG(const char *fmt, ...)
+{
+       va_list ap;
+       va_start(ap, fmt);
+       BUG_vfl(NULL, 0, fmt, ap);
+       va_end(ap);
+}
+#endif