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

[PATCH v4 2/4] die_errno(): double % in strerror() output just in case

From
Thomas Rast <trast@student.ethz.ch>
Date
Jun 27, 2009, 15:58 UTC
Message-ID
<c3635ed5bd260b5f358893917d05bd09320ae9d6.1246115205.git.trast@student.ethz.ch>
In-Reply-To
<cover.1246115204.git.trast@student.ethz.ch>
From: Junio C Hamano <gitster@pobox.com>
[tr: handle border case where % is placed at end of buffer]
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
 usage.c |   20 ++++++++++++++++++--
 1 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/usage.c b/usage.c
index fd936a1..b6aea45 100644
--- a/usage.c
+++ b/usage.c
@@ -64,8 +64,24 @@ void die_errno(const char *fmt, ...)
 {
 	va_list params;
 	char fmt_with_err[1024];
-
-	snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, strerror(errno));
+	char str_error[256], *err;
+	int i, j;
+
+	err = strerror(errno);
+	for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
+		if ((str_error[j++] = err[i++]) != '%')
+			continue;
+		if (j < sizeof(str_error) - 1) {
+			str_error[j++] = '%';
+		} else {
+			/* No room to double the '%', so we overwrite it with
+			 * '\0' below */
+			j--;
+			break;
+		}
+	}
+	str_error[j] = 0;
+	snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
 
 	va_start(params, fmt);
 	die_routine(fmt_with_err, params);
-- 
1.6.3.3.433.gc91b
Previous: Thomas RastNext: Thomas Rast
Message 11 of 14 in “die_errno()”
  1. 0/3 die_errno()Thomas Rast, Jun 8, 2009
  2. 1/3 Introduce die_errno() that appends strerror(errno) to die()Thomas Rast, Jun 8, 2009
  3. 2/3 Convert existing die(..., strerror(errno)) to die_errno()Thomas Rast, Jun 8, 2009
  4. 3/3 Use die_errno() instead of die() when checking syscallsThomas Rast, Jun 8, 2009
  5. Jeff KingJun 8, 2009
  6. Thomas RastJun 9, 2009
  7. Jeff KingJun 9, 2009
  8. Andreas EricssonJun 9, 2009
  9. 0/4 die_errno()Thomas Rast, Jun 27, 2009
  10. 1/4 Introduce die_errno() that appends strerror(errno) to die()Thomas Rast, Jun 27, 2009
  11. 2/4 die_errno(): double % in strerror() output just in caseThomas Rast, Jun 27, 2009
  12. 3/4 Convert existing die(..., strerror(errno)) to die_errno()Thomas Rast, Jun 27, 2009
  13. 4/4 Use die_errno() instead of die() when checking syscallsThomas Rast, Jun 27, 2009
  14. Junio C HamanoJun 27, 2009

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.