Skip to content

Commit cd163d4

Browse files
drafnelgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7e20105 commit cd163d4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

usage.c

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

9+
static int dying;
10+
911
void vreportf(const char *prefix, const char *err, va_list params)
1012
{
1113
char msg[4096];
@@ -82,6 +84,12 @@ void NORETURN die(const char *err, ...)
8284
{
8385
va_list params;
8486

87+
if (dying) {
88+
fputs("fatal: recursion detected in die handler\n", stderr);
89+
exit(128);
90+
}
91+
dying = 1;
92+
8593
va_start(params, err);
8694
die_routine(err, params);
8795
va_end(params);
@@ -94,6 +102,13 @@ void NORETURN die_errno(const char *fmt, ...)
94102
char str_error[256], *err;
95103
int i, j;
96104

105+
if (dying) {
106+
fputs("fatal: recursion detected in die_errno handler\n",
107+
stderr);
108+
exit(128);
109+
}
110+
dying = 1;
111+
97112
err = strerror(errno);
98113
for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
99114
if ((str_error[j++] = err[i++]) != '%')

0 commit comments

Comments
 (0)