git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Brandon Casey <drafnel@gmail.com>
Date
Nov 15, 2012, 01:45 UTC
Message-ID
<1352943952-16964-1-git-send-email-drafnel@gmail.com>

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>
---
 usage.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
diff --git a/usage.c b/usage.c
index a2a6678..8eab281 100644
--- a/usage.c
+++ b/usage.c
@@ -6,6 +6,8 @@
 #include "git-compat-util.h"
 #include "cache.h"
 
+static int dying;
+
 void vreportf(const char *prefix, const char *err, va_list params)
 {
 	char msg[4096];
@@ -82,6 +84,12 @@ void NORETURN die(const char *err, ...)
 {
 	va_list params;
 
+	if (dying) {
+		fputs("fatal: recursion detected in die handler\n", stderr);
+		exit(128);
+	}
+	dying = 1;
+
 	va_start(params, err);
 	die_routine(err, params);
 	va_end(params);
@@ -94,6 +102,13 @@ void NORETURN die_errno(const char *fmt, ...)
 	char str_error[256], *err;
 	int i, j;
 
+	if (dying) {
+		fputs("fatal: recursion detected in die_errno handler\n",
+			stderr);
+		exit(128);
+	}
+	dying = 1;
+
 	err = strerror(errno);
 	for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
 		if ((str_error[j++] = err[i++]) != '%')
-- 
1.8.0
Message 1 of 1 in “usage.c: detect recursion in die routines and bail out immediately”
  1. usage.c: detect recursion in die routines and bail out immediatelyBrandon Casey, Nov 15, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.