usage.c: detect recursion in die routines and bail out immediately

It is theoretically possible for a die handler to get into a state of infinite recursion. For example, if a die handler called another function which itself called die(). Let's at least detect this situation, inform the user, and call exit. Signed-off-by: Brandon Casey <bcasey@nvidia.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Casey committed Nov 14, 2012 at 17:45 UTC cd163d4b4e190d5e5131962c1b8f84601d4736d4
1 file changed +15
usage.c
+15
@@ -6,6 +6,8 @@
6 #include "git-compat-util.h"
7 #include "cache.h"
8
9 +static int dying;
10 +
11 void vreportf(const char *prefix, const char *err, va_list params)
12 {
13 char msg[4096];
@@ -82,6 +84,12 @@ void NORETURN die(const char *err, ...)
84 {
85 va_list params;
86
87 + if (dying) {
88 + fputs("fatal: recursion detected in die handler\n", stderr);
89 + exit(128);
90 + }
91 + dying = 1;
92 +
93 va_start(params, err);
94 die_routine(err, params);
95 va_end(params);
@@ -94,6 +102,13 @@ void NORETURN die_errno(const char *fmt, ...)
102 char str_error[256], *err;
103 int i, j;
104
105 + if (dying) {
106 + fputs("fatal: recursion detected in die_errno handler\n",
107 + stderr);
108 + exit(128);
109 + }
110 + dying = 1;
111 +
112 err = strerror(errno);
113 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
114 if ((str_error[j++] = err[i++]) != '%')