nfv?asprintf are broken without va_copy, workaround them.
* drop nfasprintf. * move nfvasprintf into imap-send.c back, and let it work on a 8k buffer, and die() in case of overflow. It should be enough for imap commands, if someone cares about imap-send, he's welcomed to fix it properly. * replace nfvasprintf use in merge-recursive with a copy of the strbuf_addf logic, it's one place, we'll live with it. To ease the change, output_buffer string list is replaced with a strbuf ;) * rework trace.c to call vsnprintf itself. It's used to format strerror()s and git command names, it should never be more than a few octets long, let it work on a 8k static buffer with vsnprintf or die loudly. Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Pierre Habouzit committed
Sep 20, 2007 at 10:43 UTC
19247e5510279f018f8358a72b38cc5aa62fac8a
4 files changed
+75
-105
cache.h
-2
@@ -585,8 +585,6 @@ extern void *alloc_object_node(void);
585
extern void alloc_report(void);
586
587
/* trace.c */
588
-extern int nfasprintf(char **str, const char *fmt, ...);
589
-extern int nfvasprintf(char **str, const char *fmt, va_list va);
588
extern void trace_printf(const char *format, ...);
589
extern void trace_argv_printf(const char **argv, int count, const char *format, ...);
590
imap-send.c
+13
@@ -105,6 +105,19 @@ static void free_generic_messages( message_t * );
105
106
static int nfsnprintf( char *buf, int blen, const char *fmt, ... );
107
108
+static int nfvasprintf(char **strp, const char *fmt, va_list ap)
109
+{
110
+ int len;
111
+ char tmp[8192];
112
+
113
+ len = vsnprintf(tmp, sizeof(tmp), fmt, ap);
114
+ if (len < 0)
115
+ die("Fatal: Out of memory\n");
116
+ if (len >= sizeof(tmp))
117
+ die("imap command overflow !\n");
118
+ *strp = xmemdupz(tmp, len);
119
+ return len;
120
+}
121
122
static void arc4_init( void );
123
static unsigned char arc4_getbyte( void );
merge-recursive.c
+35
-40
@@ -85,63 +85,58 @@ struct stage_data
85
unsigned processed:1;
86
};
87
88
-struct output_buffer
89
-{
90
- struct output_buffer *next;
91
- char *str;
92
-};
93
-
88
static struct path_list current_file_set = {NULL, 0, 0, 1};
89
static struct path_list current_directory_set = {NULL, 0, 0, 1};
90
91
static int call_depth = 0;
92
static int verbosity = 2;
93
static int buffer_output = 1;
100
-static struct output_buffer *output_list, *output_end;
94
+static struct strbuf obuf = STRBUF_INIT;
95
102
-static int show (int v)
96
+static int show(int v)
97
{
98
return (!call_depth && verbosity >= v) || verbosity >= 5;
99
}
100
107
-static void output(int v, const char *fmt, ...)
101
+static void flush_output(void)
102
{
109
- va_list args;
110
- va_start(args, fmt);
111
- if (buffer_output && show(v)) {
112
- struct output_buffer *b = xmalloc(sizeof(*b));
113
- nfvasprintf(&b->str, fmt, args);
114
- b->next = NULL;
115
- if (output_end)
116
- output_end->next = b;
117
- else
118
- output_list = b;
119
- output_end = b;
120
- } else if (show(v)) {
121
- int i;
122
- for (i = call_depth; i--;)
123
- fputs(" ", stdout);
124
- vfprintf(stdout, fmt, args);
125
- fputc('\n', stdout);
103
+ if (obuf.len) {
104
+ fputs(obuf.buf, stdout);
105
+ strbuf_reset(&obuf);
106
}
127
- va_end(args);
107
}
108
130
-static void flush_output(void)
109
+static void output(int v, const char *fmt, ...)
110
{
132
- struct output_buffer *b, *n;
133
- for (b = output_list; b; b = n) {
134
- int i;
135
- for (i = call_depth; i--;)
136
- fputs(" ", stdout);
137
- fputs(b->str, stdout);
138
- fputc('\n', stdout);
139
- n = b->next;
140
- free(b->str);
141
- free(b);
111
+ int len;
112
+ va_list ap;
113
+
114
+ if (!show(v))
115
+ return;
116
+
117
+ strbuf_grow(&obuf, call_depth * 2 + 2);
118
+ memset(obuf.buf + obuf.len, ' ', call_depth * 2);
119
+ strbuf_setlen(&obuf, obuf.len + call_depth * 2);
120
+
121
+ va_start(ap, fmt);
122
+ len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
123
+ va_end(ap);
124
+
125
+ if (len < 0)
126
+ len = 0;
127
+ if (len >= strbuf_avail(&obuf)) {
128
+ strbuf_grow(&obuf, len + 2);
129
+ va_start(ap, fmt);
130
+ len = vsnprintf(obuf.buf + obuf.len, strbuf_avail(&obuf), fmt, ap);
131
+ va_end(ap);
132
+ if (len >= strbuf_avail(&obuf)) {
133
+ die("this should not happen, your snprintf is broken");
134
+ }
135
}
143
- output_list = NULL;
144
- output_end = NULL;
136
+ strbuf_setlen(&obuf, obuf.len + len);
137
+ strbuf_add(&obuf, "\n", 1);
138
+ if (!buffer_output)
139
+ flush_output();
140
}
141
142
static void output_commit_title(struct commit *commit)
trace.c
+27
-63
@@ -25,33 +25,6 @@
25
#include "cache.h"
26
#include "quote.h"
27
28
-/* Stolen from "imap-send.c". */
29
-int nfvasprintf(char **strp, const char *fmt, va_list ap)
30
-{
31
- int len;
32
- char tmp[1024];
33
-
34
- if ((len = vsnprintf(tmp, sizeof(tmp), fmt, ap)) < 0 ||
35
- !(*strp = xmalloc(len + 1)))
36
- die("Fatal: Out of memory\n");
37
- if (len >= (int)sizeof(tmp))
38
- vsprintf(*strp, fmt, ap);
39
- else
40
- memcpy(*strp, tmp, len + 1);
41
- return len;
42
-}
43
-
44
-int nfasprintf(char **str, const char *fmt, ...)
45
-{
46
- int rc;
47
- va_list args;
48
-
49
- va_start(args, fmt);
50
- rc = nfvasprintf(str, fmt, args);
51
- va_end(args);
52
- return rc;
53
-}
54
-
28
/* Get a trace file descriptor from GIT_TRACE env variable. */
29
static int get_trace_fd(int *need_close)
30
{
@@ -89,63 +62,54 @@ static int get_trace_fd(int *need_close)
62
static const char err_msg[] = "Could not trace into fd given by "
63
"GIT_TRACE environment variable";
64
92
-void trace_printf(const char *format, ...)
65
+void trace_printf(const char *fmt, ...)
66
{
94
- char *trace_str;
95
- va_list rest;
96
- int need_close = 0;
97
- int fd = get_trace_fd(&need_close);
67
+ char buf[8192];
68
+ va_list ap;
69
+ int fd, len, need_close = 0;
70
71
+ fd = get_trace_fd(&need_close);
72
if (!fd)
73
return;
74
102
- va_start(rest, format);
103
- nfvasprintf(&trace_str, format, rest);
104
- va_end(rest);
105
-
106
- write_or_whine_pipe(fd, trace_str, strlen(trace_str), err_msg);
107
-
108
- free(trace_str);
75
+ va_start(ap, fmt);
76
+ len = vsnprintf(buf, sizeof(buf), fmt, ap);
77
+ va_end(ap);
78
+ if (len >= sizeof(buf))
79
+ die("unreasonnable trace length");
80
+ write_or_whine_pipe(fd, buf, len, err_msg);
81
82
if (need_close)
83
close(fd);
84
}
85
114
-void trace_argv_printf(const char **argv, int count, const char *format, ...)
86
+void trace_argv_printf(const char **argv, int count, const char *fmt, ...)
87
{
116
- char *argv_str, *format_str, *trace_str;
117
- size_t argv_len, format_len, trace_len;
118
- va_list rest;
119
- int need_close = 0;
120
- int fd = get_trace_fd(&need_close);
88
+ char buf[8192];
89
+ va_list ap;
90
+ char *argv_str;
91
+ size_t argv_len;
92
+ int fd, len, need_close = 0;
93
94
+ fd = get_trace_fd(&need_close);
95
if (!fd)
96
return;
97
98
+ va_start(ap, fmt);
99
+ len = vsnprintf(buf, sizeof(buf), fmt, ap);
100
+ va_end(ap);
101
+ if (len >= sizeof(buf))
102
+ die("unreasonnable trace length");
103
+
104
/* Get the argv string. */
105
argv_str = sq_quote_argv(argv, count);
106
argv_len = strlen(argv_str);
107
129
- /* Get the formated string. */
130
- va_start(rest, format);
131
- nfvasprintf(&format_str, format, rest);
132
- va_end(rest);
133
-
134
- /* Allocate buffer for trace string. */
135
- format_len = strlen(format_str);
136
- trace_len = argv_len + format_len + 1; /* + 1 for \n */
137
- trace_str = xmalloc(trace_len + 1);
138
-
139
- /* Copy everything into the trace string. */
140
- strncpy(trace_str, format_str, format_len);
141
- strncpy(trace_str + format_len, argv_str, argv_len);
142
- strcpy(trace_str + trace_len - 1, "\n");
143
-
144
- write_or_whine_pipe(fd, trace_str, trace_len, err_msg);
108
+ write_or_whine_pipe(fd, buf, len, err_msg);
109
+ write_or_whine_pipe(fd, argv_str, argv_len, err_msg);
110
+ write_or_whine_pipe(fd, "\n", 1, err_msg);
111
112
free(argv_str);
147
- free(format_str);
148
- free(trace_str);
113
114
if (need_close)
115
close(fd);