branch: fix a leak in dwim_and_setup_tracking
[alt-git.git] / usage.c
blob46d99f8bd43a2456bf59337fab2101463c66b54b
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "git-compat-util.h"
7 #include "gettext.h"
8 #include "trace2.h"
9 #include "wrapper.h"
11 static void vreportf(const char *prefix, const char *err, va_list params)
13 char msg[4096];
14 char *p, *pend = msg + sizeof(msg);
15 size_t prefix_len = strlen(prefix);
17 if (sizeof(msg) <= prefix_len) {
18 fprintf(stderr, "BUG!!! too long a prefix '%s'\n", prefix);
19 abort();
21 memcpy(msg, prefix, prefix_len);
22 p = msg + prefix_len;
23 if (vsnprintf(p, pend - p, err, params) < 0)
24 *p = '\0'; /* vsnprintf() failed, clip at prefix */
26 for (; p != pend - 1 && *p; p++) {
27 if (iscntrl(*p) && *p != '\t' && *p != '\n')
28 *p = '?';
31 *(p++) = '\n'; /* we no longer need a NUL */
32 fflush(stderr);
33 write_in_full(2, msg, p - msg);
36 static NORETURN void usage_builtin(const char *err, va_list params)
38 vreportf(_("usage: "), err, params);
41 * When we detect a usage error *before* the command dispatch in
42 * cmd_main(), we don't know what verb to report. Force it to this
43 * to facilitate post-processing.
45 trace2_cmd_name("_usage_");
48 * Currently, the (err, params) are usually just the static usage
49 * string which isn't very useful here. Usually, the call site
50 * manually calls fprintf(stderr,...) with the actual detailed
51 * syntax error before calling usage().
53 * TODO It would be nice to update the call sites to pass both
54 * the static usage string and the detailed error message.
57 exit(129);
60 static void die_message_builtin(const char *err, va_list params)
62 trace2_cmd_error_va(err, params);
63 vreportf(_("fatal: "), err, params);
67 * We call trace2_cmd_error_va() in the below functions first and
68 * expect it to va_copy 'params' before using it (because an 'ap' can
69 * only be walked once).
71 static NORETURN void die_builtin(const char *err, va_list params)
73 report_fn die_message_fn = get_die_message_routine();
75 die_message_fn(err, params);
76 exit(128);
79 static void error_builtin(const char *err, va_list params)
81 trace2_cmd_error_va(err, params);
83 vreportf(_("error: "), err, params);
86 static void warn_builtin(const char *warn, va_list params)
88 trace2_cmd_error_va(warn, params);
90 vreportf(_("warning: "), warn, params);
93 static int die_is_recursing_builtin(void)
95 static int dying;
97 * Just an arbitrary number X where "a < x < b" where "a" is
98 * "maximum number of pthreads we'll ever plausibly spawn" and
99 * "b" is "something less than Inf", since the point is to
100 * prevent infinite recursion.
102 static const int recursion_limit = 1024;
104 dying++;
105 if (dying > recursion_limit) {
106 return 1;
107 } else if (dying == 2) {
108 warning("die() called many times. Recursion error or racy threaded death!");
109 return 0;
110 } else {
111 return 0;
115 /* If we are in a dlopen()ed .so write to a global variable would segfault
116 * (ugh), so keep things static. */
117 static NORETURN_PTR report_fn usage_routine = usage_builtin;
118 static NORETURN_PTR report_fn die_routine = die_builtin;
119 static report_fn die_message_routine = die_message_builtin;
120 static report_fn error_routine = error_builtin;
121 static report_fn warn_routine = warn_builtin;
122 static int (*die_is_recursing)(void) = die_is_recursing_builtin;
124 void set_die_routine(NORETURN_PTR report_fn routine)
126 die_routine = routine;
129 report_fn get_die_message_routine(void)
131 return die_message_routine;
134 void set_error_routine(report_fn routine)
136 error_routine = routine;
139 report_fn get_error_routine(void)
141 return error_routine;
144 void set_warn_routine(report_fn routine)
146 warn_routine = routine;
149 report_fn get_warn_routine(void)
151 return warn_routine;
154 void set_die_is_recursing_routine(int (*routine)(void))
156 die_is_recursing = routine;
159 void NORETURN usagef(const char *err, ...)
161 va_list params;
163 va_start(params, err);
164 usage_routine(err, params);
165 va_end(params);
168 void NORETURN usage(const char *err)
170 usagef("%s", err);
173 void NORETURN die(const char *err, ...)
175 va_list params;
177 if (die_is_recursing()) {
178 fputs("fatal: recursion detected in die handler\n", stderr);
179 exit(128);
182 va_start(params, err);
183 die_routine(err, params);
184 va_end(params);
187 static const char *fmt_with_err(char *buf, int n, const char *fmt)
189 char str_error[256], *err;
190 int i, j;
192 err = strerror(errno);
193 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
194 if ((str_error[j++] = err[i++]) != '%')
195 continue;
196 if (j < sizeof(str_error) - 1) {
197 str_error[j++] = '%';
198 } else {
199 /* No room to double the '%', so we overwrite it with
200 * '\0' below */
201 j--;
202 break;
205 str_error[j] = 0;
206 /* Truncation is acceptable here */
207 snprintf(buf, n, "%s: %s", fmt, str_error);
208 return buf;
211 void NORETURN die_errno(const char *fmt, ...)
213 char buf[1024];
214 va_list params;
216 if (die_is_recursing()) {
217 fputs("fatal: recursion detected in die_errno handler\n",
218 stderr);
219 exit(128);
222 va_start(params, fmt);
223 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
224 va_end(params);
227 #undef die_message
228 int die_message(const char *err, ...)
230 va_list params;
232 va_start(params, err);
233 die_message_routine(err, params);
234 va_end(params);
235 return 128;
238 #undef die_message_errno
239 int die_message_errno(const char *fmt, ...)
241 char buf[1024];
242 va_list params;
244 va_start(params, fmt);
245 die_message_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
246 va_end(params);
247 return 128;
250 #undef error_errno
251 int error_errno(const char *fmt, ...)
253 char buf[1024];
254 va_list params;
256 va_start(params, fmt);
257 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
258 va_end(params);
259 return -1;
262 #undef error
263 int error(const char *err, ...)
265 va_list params;
267 va_start(params, err);
268 error_routine(err, params);
269 va_end(params);
270 return -1;
273 void warning_errno(const char *warn, ...)
275 char buf[1024];
276 va_list params;
278 va_start(params, warn);
279 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
280 va_end(params);
283 void warning(const char *warn, ...)
285 va_list params;
287 va_start(params, warn);
288 warn_routine(warn, params);
289 va_end(params);
292 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
293 int BUG_exit_code;
295 static void BUG_vfl_common(const char *file, int line, const char *fmt,
296 va_list params)
298 char prefix[256];
300 /* truncation via snprintf is OK here */
301 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
303 vreportf(prefix, fmt, params);
306 static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
308 va_list params_copy;
309 static int in_bug;
311 va_copy(params_copy, params);
312 BUG_vfl_common(file, line, fmt, params);
314 if (in_bug)
315 abort();
316 in_bug = 1;
318 trace2_cmd_error_va(fmt, params_copy);
320 if (BUG_exit_code)
321 exit(BUG_exit_code);
322 abort();
325 NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
327 va_list ap;
329 bug_called_must_BUG = 0;
331 va_start(ap, fmt);
332 BUG_vfl(file, line, fmt, ap);
333 va_end(ap);
336 int bug_called_must_BUG;
337 void bug_fl(const char *file, int line, const char *fmt, ...)
339 va_list ap;
341 bug_called_must_BUG = 1;
343 va_start(ap, fmt);
344 BUG_vfl_common(file, line, fmt, ap);
345 va_end(ap);
347 va_start(ap, fmt);
348 trace2_cmd_error_va(fmt, ap);
349 va_end(ap);
352 #ifdef SUPPRESS_ANNOTATED_LEAKS
353 void unleak_memory(const void *ptr, size_t len)
355 static struct suppressed_leak_root {
356 struct suppressed_leak_root *next;
357 char data[FLEX_ARRAY];
358 } *suppressed_leaks;
359 struct suppressed_leak_root *root;
361 FLEX_ALLOC_MEM(root, data, ptr, len);
362 root->next = suppressed_leaks;
363 suppressed_leaks = root;
365 #endif