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

Re: Infinite loop in cascade_filter_fn()

From
Carlos Martín Nieto <cmn@elego.de>
Date
Nov 25, 2011, 17:02 UTC
Message-ID
<20111125170219.GD10417@beez.lab.cmartin.tk>
In-Reply-To
<Pine.GSO.4.63.1111251705330.22588@shipon.roxen.com>
On Fri, Nov 25, 2011 at 05:14:17PM +0100, Henrik Grubbström wrote:
Show 13 quoted lines
> On Fri, 25 Nov 2011, Carlos Martín Nieto wrote:
> 
> >This patch fixes this, but I think it would still break if the LF is
> >at the end of the file. Changing the `if (!input)` to put the LF in
> >the output buffer may or may not be the right soulution. I feel like
> >this should be handled by cascade_filter_fn rather than the actual
> >filter somehow, but Junio's comment (4ae66704 'stream filter: add "no
> >more input" to the filters') suggests otherwise.
> >
> >I'm working on a cleaner patch that takes care of a bit of state, but
> >this is the general idea.
> 
> Looks good to me (and seems to work in my case).

That patch would give wrong output if the same happened at the end of a file. The attached patch should also cover this case.

Show 8 quoted lines
> Typo in the commit subject though.
> 
> >  cmn
> >--- 8< ---
> >Subject: [PATCH] convert: don't loop indefintely if at LF-to-CRLF streaming
>                                        ^^^^^^^^^^^
> This should be either "infinitely", or "indefinitely", but since we
> know that the loop won't terminate "infinitely" is to be preferred.

Thanks for noticing. I went with a different title in the end. Junio, could you consider this one for inclusion in the next RC?

--- 8< ---
Subject: [PATCH] convert: track state in LF-to-CRLF filter

There may not be enough space to store CRLF in the output. If we don't fill the buffer, then the filter will keep getting called with the same short buffer and will loop forever.

Instead, always store the CR and record there's a missing LF if necessary it so we store it in the output buffer the next time the function gets called.

Reported-by: Henrik Grubbström <grubba@roxen.com>
Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
---
 convert.c |   23 ++++++++++++++++-------
 1 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/convert.c b/convert.c
index 86e9c29..c050b86 100644
--- a/convert.c
+++ b/convert.c
@@ -880,20 +880,29 @@ static int lf_to_crlf_filter_fn(struct stream_filter *filter,
 				const char *input, size_t *isize_p,
 				char *output, size_t *osize_p)
 {
-	size_t count;
+	size_t count, o = 0;
+	static int want_lf = 0;
+
+	/* Output a pending LF if we need to */
+	if (want_lf) {
+		output[o++] = '\n';
+		want_lf = 0;
+	}
 
 	if (!input)
-		return 0; /* we do not keep any states */
+		return 0; /* We've already dealt with the state */
+
 	count = *isize_p;
 	if (count) {
-		size_t i, o;
-		for (i = o = 0; o < *osize_p && i < count; i++) {
+		size_t i;
+		for (i = 0; o < *osize_p && i < count; i++) {
 			char ch = input[i];
 			if (ch == '\n') {
-				if (o + 1 < *osize_p)
-					output[o++] = '\r';
-				else
+				output[o++] = '\r';
+				if (o >= *osize_p) {
+					want_lf = 1;
 					break;
+				}
 			}
 			output[o++] = ch;
 		}
-- 
1.7.8.rc3.31.g017d1
Previous: Henrik GrubbströmNext: Junio C Hamano
Message 5 of 16 in “Infinite loop in cascade_filter_fn()”
  1. Henrik GrubbströmNov 23, 2011
  2. Carlos Martín NietoNov 25, 2011
  3. Carlos Martín NietoNov 25, 2011
  4. Henrik GrubbströmNov 25, 2011
  5. Carlos Martín NietoNov 25, 2011
  6. Junio C HamanoNov 26, 2011
  7. Carlos Martín NietoNov 28, 2011
  8. Junio C HamanoNov 28, 2011
  9. Junio C HamanoDec 16, 2011
  10. lf_to_crlf_filter(): tell the caller we added "\n" when drainingJunio C Hamano, Dec 16, 2011
  11. Henrik GrubbströmDec 19, 2011
  12. Junio C HamanoDec 19, 2011
  13. Carlos Martín NietoDec 19, 2011
  14. Henrik GrubbströmNov 25, 2011
  15. Carlos Martín NietoNov 25, 2011
  16. Henrik GrubbströmNov 25, 2011

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.