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

Re: Increase some limits in git-mailinfo

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Feb 26, 2007, 19:10 UTC
Message-ID
<Pine.LNX.4.64.0702261107060.12485@woody.linux-foundation.org>
In-Reply-To
<Pine.LNX.4.64.0702261036550.12485@woody.linux-foundation.org>
On Mon, 26 Feb 2007, Linus Torvalds wrote:
> 
> I'm not proud of this patch, and I think we should do this better, but I 
> just had trouble with an email that Eric Biederman sent me for the kernel, 
> and this is the simplest and most obvious fix..
Ok, this is the more complicated fix for the problem.

I do *not* guarantee that this works. It worked for me single test-case, but I think somebody should double-check my logic. Does it seem sane?

It basically considers all the continuation lines to be lines of their own, and if the total line is bigger than what we can fit in it, we just truncate the result rather than stop in the middle and then get confused when we try to parse the "next" line (which is just the remainder of the first line).

It also moves the "From " and ">From " special cases into the rfc2822 header line checker helper function, because the flow seemed much more logical that way.

Is this better than my stupid one-liner that just extended the line size? I dunno..

		Linus
---
 builtin-mailinfo.c |   61 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 43 insertions(+), 18 deletions(-)
diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
index 6ee6b0b..6ccbf75 100644
--- a/builtin-mailinfo.c
+++ b/builtin-mailinfo.c
@@ -406,6 +406,11 @@ static int is_rfc2822_header(char *line)
 	 */
 	int ch;
 	char *cp = line;
+
+	/* Count mbox From headers as headers */
+	if (!memcmp(line, "From ", 5) || !memcmp(line, ">From ", 6))
+		return 1;
+
 	while ((ch = *cp++)) {
 		if (ch == ':')
 			return cp != line;
@@ -419,28 +424,48 @@ static int is_rfc2822_header(char *line)
 
 static int read_one_header_line(char *line, int sz, FILE *in)
 {
-	int ofs = 0;
-	while (ofs < sz) {
-		int peek, len;
-		if (fgets(line + ofs, sz - ofs, in) == NULL)
-			break;
-		len = eatspace(line + ofs);
-		if ((len == 0) || !is_rfc2822_header(line)) {
-			/* Re-add the newline */
-			line[ofs + len] = '\n';
-			line[ofs + len + 1] = '\0';
-			break;
-		}
-		ofs += len;
-		/* Yuck, 2822 header "folding" */
+	int len;
+
+	/* Get the first part of the line.. */
+	if (!fgets(line, sz, in))
+		return 0;
+
+	/*
+	 * Is it an empty line or not a valid rfc2822 header?
+	 * If so, stop here, and return false ("not a header")
+	 */
+	len = eatspace(line);
+	if (!len || !is_rfc2822_header(line)) {
+		/* Re-add the newline */
+		line[len] = '\n';
+		line[len + 1] = '\0';
+		return 0;
+	}
+
+	/*
+	 * Now we need to eat all the continuation lines..
+	 * Yuck, 2822 header "folding"
+	 */
+	for (;;) {
+		int peek, addlen;
+		static char continuation[1000];
+
 		peek = fgetc(in); ungetc(peek, in);
 		if (peek != ' ' && peek != '\t')
 			break;
+		if (!fgets(continuation, sizeof(continuation), in))
+			break;
+		addlen = eatspace(continuation);
+		if (len < sz) {
+			if (addlen >= sz - len)
+				addlen = sz - len - 1;
+			memcpy(line + len, continuation, addlen);
+			len += addlen;
+		}
 	}
-	/* Count mbox From headers as headers */
-	if (!ofs && (!memcmp(line, "From ", 5) || !memcmp(line, ">From ", 6)))
-		ofs = 1;
-	return ofs;
+	line[len] = 0;
+
+	return 1;
 }
 
 static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
Previous: Linus TorvaldsNext: Johannes Schindelin
Message 2 of 8 in “Increase some limits in git-mailinfo”
  1. Linus TorvaldsFeb 26, 2007
  2. Linus TorvaldsFeb 26, 2007
  3. Johannes SchindelinFeb 26, 2007
  4. Linus TorvaldsFeb 26, 2007
  5. Johannes SchindelinFeb 26, 2007
  6. Linus TorvaldsFeb 26, 2007
  7. Johannes SchindelinFeb 26, 2007
  8. Linus TorvaldsFeb 26, 2007

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.