path: root/usage.c
diff options
authorJeff King <>2015-08-11 18:13:59 (GMT)
committerJunio C Hamano <>2015-08-11 21:29:36 (GMT)
commitf4c3edc0b156362a92bf9de4f0ec794e90a757fc (patch)
tree6378c196e729a486bda2bae158a671e4d04494dc /usage.c
parent3b331e92671469614662830402103848a8004b97 (diff)
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 <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'usage.c')
1 files changed, 12 insertions, 3 deletions
diff --git a/usage.c b/usage.c
index e4fa6d2..82ff131 100644
--- a/usage.c
+++ b/usage.c
@@ -7,13 +7,21 @@
#include "cache.h"
static FILE *error_handle;
+static int tweaked_error_buffering;
void vreportf(const char *prefix, const char *err, va_list params)
- char msg[4096];
FILE *fh = error_handle ? error_handle : stderr;
- vsnprintf(msg, sizeof(msg), err, params);
- fprintf(fh, "%s%s\n", prefix, msg);
+ fflush(fh);
+ if (!tweaked_error_buffering) {
+ setvbuf(fh, NULL, _IOLBF, 0);
+ tweaked_error_buffering = 1;
+ }
+ fputs(prefix, fh);
+ vfprintf(fh, err, params);
+ fputc('\n', fh);
static NORETURN void usage_builtin(const char *err, va_list params)
@@ -70,6 +78,7 @@ void set_die_is_recursing_routine(int (*routine)(void))
void set_error_handle(FILE *fh)
error_handle = fh;
+ tweaked_error_buffering = 0;
void NORETURN usagef(const char *err, ...)