2005-04-19 04:04:43 +08:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
2005-12-06 03:54:29 +08:00
|
|
|
#include "git-compat-util.h"
|
2011-07-28 05:32:34 +08:00
|
|
|
#include "cache.h"
|
2005-04-19 04:04:43 +08:00
|
|
|
|
vreportf: report to arbitrary filehandles
The vreportf function always goes to stderr, but run-command
wants child errors to go to the parent's original stderr. To
solve this, commit a5487dd duplicates the stderr fd and
installs die and error handlers to direct the output
appropriately (which later turned into the vwritef
function). This has two downsides, though:
- we make multiple calls to write(), which contradicts the
"write at once" logic from d048a96 (print
warning/error/fatal messages in one shot, 2007-11-09).
- the custom handlers basically duplicate the normal
handlers. They're only a few lines of code, but we
should not have to repeat the magic "exit(128)", for
example.
We can solve the first by using fdopen() on the duplicated
descriptor. We can't pass this to vreportf, but we could
introduce a new vreportf_to to handle it.
However, to fix the second problem, we instead introduce a
new "set_error_handle" function, which lets the normal
vreportf calls output to a handle besides stderr. Thus we
can get rid of our custom handlers entirely, and just ask
the regular handlers to output to our new descriptor.
And as vwritef has no more callers, it can just go away.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-08-12 02:06:15 +08:00
|
|
|
static FILE *error_handle;
|
|
|
|
|
2010-03-06 23:40:39 +08:00
|
|
|
void vreportf(const char *prefix, const char *err, va_list params)
|
2005-04-19 04:04:43 +08:00
|
|
|
{
|
2017-01-11 22:02:03 +08:00
|
|
|
char msg[4096];
|
vreportf: report to arbitrary filehandles
The vreportf function always goes to stderr, but run-command
wants child errors to go to the parent's original stderr. To
solve this, commit a5487dd duplicates the stderr fd and
installs die and error handlers to direct the output
appropriately (which later turned into the vwritef
function). This has two downsides, though:
- we make multiple calls to write(), which contradicts the
"write at once" logic from d048a96 (print
warning/error/fatal messages in one shot, 2007-11-09).
- the custom handlers basically duplicate the normal
handlers. They're only a few lines of code, but we
should not have to repeat the magic "exit(128)", for
example.
We can solve the first by using fdopen() on the duplicated
descriptor. We can't pass this to vreportf, but we could
introduce a new vreportf_to to handle it.
However, to fix the second problem, we instead introduce a
new "set_error_handle" function, which lets the normal
vreportf calls output to a handle besides stderr. Thus we
can get rid of our custom handlers entirely, and just ask
the regular handlers to output to our new descriptor.
And as vwritef has no more callers, it can just go away.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-08-12 02:06:15 +08:00
|
|
|
FILE *fh = error_handle ? error_handle : stderr;
|
vreport: sanitize ASCII control chars
Our error() and die() calls may report messages with
arbitrary data (e.g., filenames or even data from a remote
server). Let's make it harder to cause confusion with
mischievous filenames. E.g., try:
git rev-parse "$(printf "\rfatal: this argument is too sneaky")" --
or
git rev-parse "$(printf "\x1b[5mblinky\x1b[0m")" --
Let's block all ASCII control characters, with the exception
of TAB and LF. We use both in our own messages (and we are
necessarily sanitizing the complete output of snprintf here,
as we do not have access to the individual varargs). And TAB
and LF are unlikely to cause confusion (you could put
"\nfatal: sneaky\n" in your filename, but it would at least
not _cover up_ the message leading to it, unlike "\r").
We'll replace the characters with a "?", which is similar to
how "ls" behaves. It might be nice to do something less
lossy, like converting them to "\x" hex codes. But replacing
with a single character makes it easy to do in-place and
without worrying about length limitations. This feature
should kick in rarely enough that the "?" marks are almost
never seen.
We'll leave high-bit characters as-is, as they are likely to
be UTF-8 (though there may be some Unicode mischief you
could cause, which may require further patches).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-01-11 22:02:23 +08:00
|
|
|
char *p;
|
vreportf: avoid intermediate buffer
When we call "die(fmt, args...)", we end up in vreportf with
two pieces of information:
1. The prefix "fatal: "
2. The original fmt and va_list of args.
We format item (2) into a temporary buffer, and then fprintf
the prefix and the temporary buffer, along with a newline.
This has the unfortunate side effect of truncating any error
messages that are longer than 4096 bytes.
Instead, let's use separate calls for the prefix and
newline, letting us hand the item (2) directly to vfprintf.
This is essentially undoing d048a96 (print
warning/error/fatal messages in one shot, 2007-11-09), which
tried to have the whole output end up in a single `write`
call.
But we can address this instead by explicitly requesting
line-buffering for the output handle, and by making sure
that the buffer is empty before we start (so that outputting
the prefix does not cause a flush due to hitting the buffer
limit).
We may still break the output into two writes if the content
is larger than our buffer, but there's not much we can do
there; depending on the stdio implementation, that might
have happened even with a single fprintf call.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-08-12 02:13:59 +08:00
|
|
|
|
2017-01-11 22:02:03 +08:00
|
|
|
vsnprintf(msg, sizeof(msg), err, params);
|
vreport: sanitize ASCII control chars
Our error() and die() calls may report messages with
arbitrary data (e.g., filenames or even data from a remote
server). Let's make it harder to cause confusion with
mischievous filenames. E.g., try:
git rev-parse "$(printf "\rfatal: this argument is too sneaky")" --
or
git rev-parse "$(printf "\x1b[5mblinky\x1b[0m")" --
Let's block all ASCII control characters, with the exception
of TAB and LF. We use both in our own messages (and we are
necessarily sanitizing the complete output of snprintf here,
as we do not have access to the individual varargs). And TAB
and LF are unlikely to cause confusion (you could put
"\nfatal: sneaky\n" in your filename, but it would at least
not _cover up_ the message leading to it, unlike "\r").
We'll replace the characters with a "?", which is similar to
how "ls" behaves. It might be nice to do something less
lossy, like converting them to "\x" hex codes. But replacing
with a single character makes it easy to do in-place and
without worrying about length limitations. This feature
should kick in rarely enough that the "?" marks are almost
never seen.
We'll leave high-bit characters as-is, as they are likely to
be UTF-8 (though there may be some Unicode mischief you
could cause, which may require further patches).
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2017-01-11 22:02:23 +08:00
|
|
|
for (p = msg; *p; p++) {
|
|
|
|
if (iscntrl(*p) && *p != '\t' && *p != '\n')
|
|
|
|
*p = '?';
|
vreportf: avoid intermediate buffer
When we call "die(fmt, args...)", we end up in vreportf with
two pieces of information:
1. The prefix "fatal: "
2. The original fmt and va_list of args.
We format item (2) into a temporary buffer, and then fprintf
the prefix and the temporary buffer, along with a newline.
This has the unfortunate side effect of truncating any error
messages that are longer than 4096 bytes.
Instead, let's use separate calls for the prefix and
newline, letting us hand the item (2) directly to vfprintf.
This is essentially undoing d048a96 (print
warning/error/fatal messages in one shot, 2007-11-09), which
tried to have the whole output end up in a single `write`
call.
But we can address this instead by explicitly requesting
line-buffering for the output handle, and by making sure
that the buffer is empty before we start (so that outputting
the prefix does not cause a flush due to hitting the buffer
limit).
We may still break the output into two writes if the content
is larger than our buffer, but there's not much we can do
there; depending on the stdio implementation, that might
have happened even with a single fprintf call.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-08-12 02:13:59 +08:00
|
|
|
}
|
2017-01-11 22:02:03 +08:00
|
|
|
fprintf(fh, "%s%s\n", prefix, msg);
|
2011-07-28 05:32:34 +08:00
|
|
|
}
|
|
|
|
|
2009-11-09 23:05:02 +08:00
|
|
|
static NORETURN void usage_builtin(const char *err, va_list params)
|
2005-04-19 04:04:43 +08:00
|
|
|
{
|
2010-03-06 23:40:39 +08:00
|
|
|
vreportf("usage: ", err, params);
|
2005-10-02 04:24:27 +08:00
|
|
|
exit(129);
|
2005-04-19 04:04:43 +08:00
|
|
|
}
|
|
|
|
|
2006-06-24 13:44:33 +08:00
|
|
|
static NORETURN void die_builtin(const char *err, va_list params)
|
2006-06-24 10:34:38 +08:00
|
|
|
{
|
2010-03-06 23:40:39 +08:00
|
|
|
vreportf("fatal: ", err, params);
|
2006-06-24 10:34:38 +08:00
|
|
|
exit(128);
|
|
|
|
}
|
|
|
|
|
2006-06-24 13:44:33 +08:00
|
|
|
static void error_builtin(const char *err, va_list params)
|
2006-06-24 10:34:38 +08:00
|
|
|
{
|
2010-03-06 23:40:39 +08:00
|
|
|
vreportf("error: ", err, params);
|
2006-06-24 10:34:38 +08:00
|
|
|
}
|
|
|
|
|
2006-12-22 08:48:32 +08:00
|
|
|
static void warn_builtin(const char *warn, va_list params)
|
|
|
|
{
|
2010-03-06 23:40:39 +08:00
|
|
|
vreportf("warning: ", warn, params);
|
2006-12-22 08:48:32 +08:00
|
|
|
}
|
2006-06-24 10:34:38 +08:00
|
|
|
|
2013-04-17 03:46:22 +08:00
|
|
|
static int die_is_recursing_builtin(void)
|
|
|
|
{
|
|
|
|
static int dying;
|
|
|
|
return dying++;
|
|
|
|
}
|
|
|
|
|
2006-06-24 10:34:38 +08:00
|
|
|
/* If we are in a dlopen()ed .so write to a global variable would segfault
|
|
|
|
* (ugh), so keep things static. */
|
2009-11-09 23:05:02 +08:00
|
|
|
static NORETURN_PTR void (*usage_routine)(const char *err, va_list params) = usage_builtin;
|
2009-10-01 02:05:50 +08:00
|
|
|
static NORETURN_PTR void (*die_routine)(const char *err, va_list params) = die_builtin;
|
2006-06-24 10:34:38 +08:00
|
|
|
static void (*error_routine)(const char *err, va_list params) = error_builtin;
|
2006-12-22 08:48:32 +08:00
|
|
|
static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
|
2013-04-17 03:46:22 +08:00
|
|
|
static int (*die_is_recursing)(void) = die_is_recursing_builtin;
|
2006-06-24 10:34:38 +08:00
|
|
|
|
2009-10-01 02:05:50 +08:00
|
|
|
void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params))
|
2006-06-24 10:34:38 +08:00
|
|
|
{
|
|
|
|
die_routine = routine;
|
|
|
|
}
|
|
|
|
|
2011-07-28 05:32:34 +08:00
|
|
|
void set_error_routine(void (*routine)(const char *err, va_list params))
|
|
|
|
{
|
|
|
|
error_routine = routine;
|
|
|
|
}
|
|
|
|
|
2016-09-05 04:18:28 +08:00
|
|
|
void (*get_error_routine(void))(const char *err, va_list params)
|
|
|
|
{
|
|
|
|
return error_routine;
|
|
|
|
}
|
|
|
|
|
2016-09-05 04:18:27 +08:00
|
|
|
void set_warn_routine(void (*routine)(const char *warn, va_list params))
|
|
|
|
{
|
|
|
|
warn_routine = routine;
|
|
|
|
}
|
|
|
|
|
2016-09-05 04:18:28 +08:00
|
|
|
void (*get_warn_routine(void))(const char *warn, va_list params)
|
|
|
|
{
|
|
|
|
return warn_routine;
|
|
|
|
}
|
|
|
|
|
2013-04-17 03:46:22 +08:00
|
|
|
void set_die_is_recursing_routine(int (*routine)(void))
|
|
|
|
{
|
|
|
|
die_is_recursing = routine;
|
|
|
|
}
|
|
|
|
|
vreportf: report to arbitrary filehandles
The vreportf function always goes to stderr, but run-command
wants child errors to go to the parent's original stderr. To
solve this, commit a5487dd duplicates the stderr fd and
installs die and error handlers to direct the output
appropriately (which later turned into the vwritef
function). This has two downsides, though:
- we make multiple calls to write(), which contradicts the
"write at once" logic from d048a96 (print
warning/error/fatal messages in one shot, 2007-11-09).
- the custom handlers basically duplicate the normal
handlers. They're only a few lines of code, but we
should not have to repeat the magic "exit(128)", for
example.
We can solve the first by using fdopen() on the duplicated
descriptor. We can't pass this to vreportf, but we could
introduce a new vreportf_to to handle it.
However, to fix the second problem, we instead introduce a
new "set_error_handle" function, which lets the normal
vreportf calls output to a handle besides stderr. Thus we
can get rid of our custom handlers entirely, and just ask
the regular handlers to output to our new descriptor.
And as vwritef has no more callers, it can just go away.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2015-08-12 02:06:15 +08:00
|
|
|
void set_error_handle(FILE *fh)
|
|
|
|
{
|
|
|
|
error_handle = fh;
|
|
|
|
}
|
|
|
|
|
Fix sparse warnings
Fix warnings from 'make check'.
- These files don't include 'builtin.h' causing sparse to complain that
cmd_* isn't declared:
builtin/clone.c:364, builtin/fetch-pack.c:797,
builtin/fmt-merge-msg.c:34, builtin/hash-object.c:78,
builtin/merge-index.c:69, builtin/merge-recursive.c:22
builtin/merge-tree.c:341, builtin/mktag.c:156, builtin/notes.c:426
builtin/notes.c:822, builtin/pack-redundant.c:596,
builtin/pack-refs.c:10, builtin/patch-id.c:60, builtin/patch-id.c:149,
builtin/remote.c:1512, builtin/remote-ext.c:240,
builtin/remote-fd.c:53, builtin/reset.c:236, builtin/send-pack.c:384,
builtin/unpack-file.c:25, builtin/var.c:75
- These files have symbols which should be marked static since they're
only file scope:
submodule.c:12, diff.c:631, replace_object.c:92, submodule.c:13,
submodule.c:14, trace.c:78, transport.c:195, transport-helper.c:79,
unpack-trees.c:19, url.c:3, url.c:18, url.c:104, url.c:117, url.c:123,
url.c:129, url.c:136, thread-utils.c:21, thread-utils.c:48
- These files redeclare symbols to be different types:
builtin/index-pack.c:210, parse-options.c:564, parse-options.c:571,
usage.c:49, usage.c:58, usage.c:63, usage.c:72
- These files use a literal integer 0 when they really should use a NULL
pointer:
daemon.c:663, fast-import.c:2942, imap-send.c:1072, notes-merge.c:362
While we're in the area, clean up some unused #includes in builtin files
(mostly exec_cmd.h).
Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-22 15:51:05 +08:00
|
|
|
void NORETURN usagef(const char *err, ...)
|
2009-11-09 23:05:02 +08:00
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, err);
|
|
|
|
usage_routine(err, params);
|
|
|
|
va_end(params);
|
|
|
|
}
|
|
|
|
|
Fix sparse warnings
Fix warnings from 'make check'.
- These files don't include 'builtin.h' causing sparse to complain that
cmd_* isn't declared:
builtin/clone.c:364, builtin/fetch-pack.c:797,
builtin/fmt-merge-msg.c:34, builtin/hash-object.c:78,
builtin/merge-index.c:69, builtin/merge-recursive.c:22
builtin/merge-tree.c:341, builtin/mktag.c:156, builtin/notes.c:426
builtin/notes.c:822, builtin/pack-redundant.c:596,
builtin/pack-refs.c:10, builtin/patch-id.c:60, builtin/patch-id.c:149,
builtin/remote.c:1512, builtin/remote-ext.c:240,
builtin/remote-fd.c:53, builtin/reset.c:236, builtin/send-pack.c:384,
builtin/unpack-file.c:25, builtin/var.c:75
- These files have symbols which should be marked static since they're
only file scope:
submodule.c:12, diff.c:631, replace_object.c:92, submodule.c:13,
submodule.c:14, trace.c:78, transport.c:195, transport-helper.c:79,
unpack-trees.c:19, url.c:3, url.c:18, url.c:104, url.c:117, url.c:123,
url.c:129, url.c:136, thread-utils.c:21, thread-utils.c:48
- These files redeclare symbols to be different types:
builtin/index-pack.c:210, parse-options.c:564, parse-options.c:571,
usage.c:49, usage.c:58, usage.c:63, usage.c:72
- These files use a literal integer 0 when they really should use a NULL
pointer:
daemon.c:663, fast-import.c:2942, imap-send.c:1072, notes-merge.c:362
While we're in the area, clean up some unused #includes in builtin files
(mostly exec_cmd.h).
Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-22 15:51:05 +08:00
|
|
|
void NORETURN usage(const char *err)
|
2006-06-24 10:34:38 +08:00
|
|
|
{
|
2009-11-09 23:05:02 +08:00
|
|
|
usagef("%s", err);
|
2006-06-24 10:34:38 +08:00
|
|
|
}
|
|
|
|
|
Fix sparse warnings
Fix warnings from 'make check'.
- These files don't include 'builtin.h' causing sparse to complain that
cmd_* isn't declared:
builtin/clone.c:364, builtin/fetch-pack.c:797,
builtin/fmt-merge-msg.c:34, builtin/hash-object.c:78,
builtin/merge-index.c:69, builtin/merge-recursive.c:22
builtin/merge-tree.c:341, builtin/mktag.c:156, builtin/notes.c:426
builtin/notes.c:822, builtin/pack-redundant.c:596,
builtin/pack-refs.c:10, builtin/patch-id.c:60, builtin/patch-id.c:149,
builtin/remote.c:1512, builtin/remote-ext.c:240,
builtin/remote-fd.c:53, builtin/reset.c:236, builtin/send-pack.c:384,
builtin/unpack-file.c:25, builtin/var.c:75
- These files have symbols which should be marked static since they're
only file scope:
submodule.c:12, diff.c:631, replace_object.c:92, submodule.c:13,
submodule.c:14, trace.c:78, transport.c:195, transport-helper.c:79,
unpack-trees.c:19, url.c:3, url.c:18, url.c:104, url.c:117, url.c:123,
url.c:129, url.c:136, thread-utils.c:21, thread-utils.c:48
- These files redeclare symbols to be different types:
builtin/index-pack.c:210, parse-options.c:564, parse-options.c:571,
usage.c:49, usage.c:58, usage.c:63, usage.c:72
- These files use a literal integer 0 when they really should use a NULL
pointer:
daemon.c:663, fast-import.c:2942, imap-send.c:1072, notes-merge.c:362
While we're in the area, clean up some unused #includes in builtin files
(mostly exec_cmd.h).
Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2011-03-22 15:51:05 +08:00
|
|
|
void NORETURN die(const char *err, ...)
|
2005-04-19 04:04:43 +08:00
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
2013-04-17 03:46:22 +08:00
|
|
|
if (die_is_recursing()) {
|
2012-11-15 09:45:52 +08:00
|
|
|
fputs("fatal: recursion detected in die handler\n", stderr);
|
|
|
|
exit(128);
|
|
|
|
}
|
|
|
|
|
2005-04-19 04:04:43 +08:00
|
|
|
va_start(params, err);
|
2006-06-24 10:34:38 +08:00
|
|
|
die_routine(err, params);
|
2005-04-19 04:04:43 +08:00
|
|
|
va_end(params);
|
|
|
|
}
|
|
|
|
|
2016-05-08 17:47:21 +08:00
|
|
|
static const char *fmt_with_err(char *buf, int n, const char *fmt)
|
2009-06-27 23:58:44 +08:00
|
|
|
{
|
2009-06-27 23:58:45 +08:00
|
|
|
char str_error[256], *err;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
err = strerror(errno);
|
|
|
|
for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
|
|
|
|
if ((str_error[j++] = err[i++]) != '%')
|
|
|
|
continue;
|
|
|
|
if (j < sizeof(str_error) - 1) {
|
|
|
|
str_error[j++] = '%';
|
|
|
|
} else {
|
|
|
|
/* No room to double the '%', so we overwrite it with
|
|
|
|
* '\0' below */
|
|
|
|
j--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str_error[j] = 0;
|
2016-05-08 17:47:21 +08:00
|
|
|
snprintf(buf, n, "%s: %s", fmt, str_error);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NORETURN die_errno(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
if (die_is_recursing()) {
|
|
|
|
fputs("fatal: recursion detected in die_errno handler\n",
|
|
|
|
stderr);
|
|
|
|
exit(128);
|
|
|
|
}
|
2009-06-27 23:58:44 +08:00
|
|
|
|
|
|
|
va_start(params, fmt);
|
2016-05-08 17:47:21 +08:00
|
|
|
die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
|
2009-06-27 23:58:44 +08:00
|
|
|
va_end(params);
|
|
|
|
}
|
|
|
|
|
2016-08-31 11:41:22 +08:00
|
|
|
#undef error_errno
|
2016-05-08 17:47:22 +08:00
|
|
|
int error_errno(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, fmt);
|
|
|
|
error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
|
|
|
|
va_end(params);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
make error()'s constant return value more visible
When git is compiled with "gcc -Wuninitialized -O3", some
inlined calls provide an additional opportunity for the
compiler to do static analysis on variable initialization.
For example, with two functions like this:
int get_foo(int *foo)
{
if (something_that_might_fail() < 0)
return error("unable to get foo");
*foo = 0;
return 0;
}
void some_fun(void)
{
int foo;
if (get_foo(&foo) < 0)
return -1;
printf("foo is %d\n", foo);
}
If get_foo() is not inlined, then when compiling some_fun,
gcc sees only that a pointer to the local variable is
passed, and must assume that it is an out parameter that
is initialized after get_foo returns.
However, when get_foo() is inlined, the compiler may look at
all of the code together and see that some code paths in
get_foo() do not initialize the variable. As a result, it
prints a warning. But what the compiler can't see is that
error() always returns -1, and therefore we know that either
we return early from some_fun, or foo ends up initialized,
and the code is safe. The warning is a false positive.
If we can make the compiler aware that error() will always
return -1, it can do a better job of analysis. The simplest
method would be to inline the error() function. However,
this doesn't work, because gcc will not inline a variadc
function. We can work around this by defining a macro. This
relies on two gcc extensions:
1. Variadic macros (these are present in C99, but we do
not rely on that).
2. Gcc treats the "##" paste operator specially between a
comma and __VA_ARGS__, which lets our variadic macro
work even if no format parameters are passed to
error().
Since we are using these extra features, we hide the macro
behind an #ifdef. This is OK, though, because our goal was
just to help gcc.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-12-16 01:37:36 +08:00
|
|
|
#undef error
|
2005-04-19 04:04:43 +08:00
|
|
|
int error(const char *err, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, err);
|
2006-06-24 10:34:38 +08:00
|
|
|
error_routine(err, params);
|
2005-04-19 04:04:43 +08:00
|
|
|
va_end(params);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-22 08:48:32 +08:00
|
|
|
|
2016-05-08 17:47:22 +08:00
|
|
|
void warning_errno(const char *warn, ...)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, warn);
|
|
|
|
warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
|
|
|
|
va_end(params);
|
|
|
|
}
|
|
|
|
|
2007-03-31 07:07:05 +08:00
|
|
|
void warning(const char *warn, ...)
|
2006-12-22 08:48:32 +08:00
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, warn);
|
|
|
|
warn_routine(warn, params);
|
|
|
|
va_end(params);
|
|
|
|
}
|
usage.c: add BUG() function
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>
2017-05-13 11:28:50 +08:00
|
|
|
|
|
|
|
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
|