Skip to content

Commit 1302c9f

Browse files
committed
Merge branch 'jk/long-error-messages'
The codepath to produce error messages had a hard-coded limit to the size of the message, primarily to avoid memory allocation while calling die(). * jk/long-error-messages: vreportf: avoid intermediate buffer vreportf: report to arbitrary filehandles
2 parents b590720 + f4c3edc commit 1302c9f

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

git-compat-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ struct strbuf;
389389

390390
/* General helper functions */
391391
extern void vreportf(const char *prefix, const char *err, va_list params);
392-
extern void vwritef(int fd, const char *prefix, const char *err, va_list params);
393392
extern NORETURN void usage(const char *err);
394393
extern NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
395394
extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
@@ -425,6 +424,7 @@ static inline int const_error(void)
425424
extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
426425
extern void set_error_routine(void (*routine)(const char *err, va_list params));
427426
extern void set_die_is_recursing_routine(int (*routine)(void));
427+
extern void set_error_handle(FILE *);
428428

429429
extern int starts_with(const char *str, const char *prefix);
430430

run-command.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ static int execv_shell_cmd(const char **argv)
200200
#endif
201201

202202
#ifndef GIT_WINDOWS_NATIVE
203-
static int child_err = 2;
204203
static int child_notifier = -1;
205204

206205
static void notify_parent(void)
@@ -212,17 +211,6 @@ static void notify_parent(void)
212211
*/
213212
xwrite(child_notifier, "", 1);
214213
}
215-
216-
static NORETURN void die_child(const char *err, va_list params)
217-
{
218-
vwritef(child_err, "fatal: ", err, params);
219-
exit(128);
220-
}
221-
222-
static void error_child(const char *err, va_list params)
223-
{
224-
vwritef(child_err, "error: ", err, params);
225-
}
226214
#endif
227215

228216
static inline void set_cloexec(int fd)
@@ -362,11 +350,10 @@ int start_command(struct child_process *cmd)
362350
* in subsequent call paths use the parent's stderr.
363351
*/
364352
if (cmd->no_stderr || need_err) {
365-
child_err = dup(2);
353+
int child_err = dup(2);
366354
set_cloexec(child_err);
355+
set_error_handle(fdopen(child_err, "w"));
367356
}
368-
set_die_routine(die_child);
369-
set_error_routine(error_child);
370357

371358
close(notify_pipe[0]);
372359
set_cloexec(notify_pipe[1]);

usage.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66
#include "git-compat-util.h"
77
#include "cache.h"
88

9+
static FILE *error_handle;
10+
static int tweaked_error_buffering;
11+
912
void vreportf(const char *prefix, const char *err, va_list params)
1013
{
11-
char msg[4096];
12-
vsnprintf(msg, sizeof(msg), err, params);
13-
fprintf(stderr, "%s%s\n", prefix, msg);
14-
}
14+
FILE *fh = error_handle ? error_handle : stderr;
1515

16-
void vwritef(int fd, const char *prefix, const char *err, va_list params)
17-
{
18-
char msg[4096];
19-
int len = vsnprintf(msg, sizeof(msg), err, params);
20-
if (len > sizeof(msg))
21-
len = sizeof(msg);
16+
fflush(fh);
17+
if (!tweaked_error_buffering) {
18+
setvbuf(fh, NULL, _IOLBF, 0);
19+
tweaked_error_buffering = 1;
20+
}
2221

23-
write_in_full(fd, prefix, strlen(prefix));
24-
write_in_full(fd, msg, len);
25-
write_in_full(fd, "\n", 1);
22+
fputs(prefix, fh);
23+
vfprintf(fh, err, params);
24+
fputc('\n', fh);
2625
}
2726

2827
static NORETURN void usage_builtin(const char *err, va_list params)
@@ -76,6 +75,12 @@ void set_die_is_recursing_routine(int (*routine)(void))
7675
die_is_recursing = routine;
7776
}
7877

78+
void set_error_handle(FILE *fh)
79+
{
80+
error_handle = fh;
81+
tweaked_error_buffering = 0;
82+
}
83+
7984
void NORETURN usagef(const char *err, ...)
8085
{
8186
va_list params;

0 commit comments

Comments
 (0)