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

Re: [PATCH v2] Let mailsplit and mailinfo handle mails with CRLF line-endings

From
BCBrandon Casey <brandon.casey.ctr@nrlssc.navy.mil>
Date
Aug 4, 2009, 21:29 UTC
Message-ID
<H9KDJzU0XgndJHC8J4OETn7kxYhGP3gl0YJPR0low0hfIHjZOUsoxw@cipher.nrlssc.navy.mil>
In-Reply-To
<20090804210327.GA23747@blimp.localdomain>
Alex Riesen wrote:
Show 30 quoted lines
> Noticed by H. Peter Anvin.
> 
> It is not that uncommon to have mails with DOS line-ending,
> notably Thunderbird and web mailers like Gmail (when saving
> what they call "original" message).
> 
> Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
> ---
> 
> Corrected bug with unconditonal last (or very long) line shortening if
> it contains a CR in next-to-last character. Noticed by Sverre Rabbelier.
> 
> It should also handle the case mentioned by Brandon Casey.
> 
>  builtin-mailsplit.c |    2 ++
>  1 files changed, 2 insertions(+), 0 deletions(-)
> 
> diff --git a/builtin-mailsplit.c b/builtin-mailsplit.c
> index ad5f6b5..48285a0 100644
> --- a/builtin-mailsplit.c
> +++ b/builtin-mailsplit.c
> @@ -58,6 +58,8 @@ int read_line_with_nul(char *buf, int size, FILE *in)
>  		if (c == '\n' || len + 1 >= size)
>  			break;
>  	}
> +	if (c == '\n' && len > 1 && buf[len - 2] == '\r')
> +		buf[--len - 1] = '\n';
>  	buf[len] = '\0';
>  
>  	return len;

What if \r lands at character 99 and \n is at character 100? If buf has exactly 100 characters available for writing. Wouldn't the '\r' be stored into buf, but not the '\n'? Then the 'for' loop would terminate since len + 1 would be >= size and the code above would test whether c == '\n', and it would not, so the '\r' would not be removed from buf as it should be.

At the point where buf has been filled, and the last character read is a '\r', we can not tell whether the next character is a '\n' or not, so we do not know if it is a solitary '\r' or whether it is the start of a '\r\n' sequence. It seems to me that we must push the '\r' back into the stream and allow the next call to read_line_with_nul() handle it, or peek to see if the next character is a '\n'.

Be aware that if we use the version I suggested which pushes the '\r' back into the input stream, I think we risk an infinite loop if size == 1. I don't think that is possible from the current callers though.

-brandon
Previous: Alex RiesenNext: Brandon Casey
Message 16 of 33 in “Please make git-am handle \r\n-damaged patches”
  1. H. Peter AnvinAug 3, 2009
  2. Junio C HamanoAug 3, 2009
  3. Wesley J. LandakerAug 3, 2009
  4. H. Peter AnvinAug 3, 2009
  5. H. Peter AnvinAug 3, 2009
  6. Sverre RabbelierAug 3, 2009
  7. H. Peter AnvinAug 3, 2009
  8. Paolo BonziniAug 4, 2009
  9. Alex RiesenAug 4, 2009
  10. Junio C HamanoAug 4, 2009
  11. Let mailsplit and mailinfo handle mails with CRLF line-endingsAlex Riesen, Aug 4, 2009
  12. Sverre RabbelierAug 4, 2009
  13. Brandon CaseyAug 4, 2009
  14. Alex RiesenAug 4, 2009
  15. Let mailsplit and mailinfo handle mails with CRLF line-endingsAlex Riesen, Aug 4, 2009
  16. Brandon CaseyAug 4, 2009
  17. Brandon CaseyAug 4, 2009
  18. Alex RiesenAug 4, 2009
  19. Alex RiesenAug 4, 2009
  20. Alex RiesenAug 4, 2009
  21. Brandon CaseyAug 4, 2009
  22. Erik Faye-LundAug 4, 2009
  23. Alex RiesenAug 4, 2009
  24. Erik Faye-LundAug 4, 2009
  25. Tony FinchAug 5, 2009
  26. Nanako ShiraishiAug 4, 2009
  27. Junio C HamanoAug 4, 2009
  28. 0/4 Re: Please make git-am handle \r\n-damaged patchesBrandon Casey, Aug 5, 2009
  29. 1/4 strbuf: add new function strbuf_getwholeline()Brandon Casey, Aug 5, 2009
  30. 2/4 builtin-mailinfo,builtin-mailsplit: use strbufsBrandon Casey, Aug 5, 2009
  31. 3/4 builtin-mailsplit.c: remove read_line_with_nul() since it is no longer usedBrandon Casey, Aug 5, 2009
  32. 4/4 Allow mailsplit (and hence git-am) to handle mails with CRLF line-endingsBrandon Casey, Aug 5, 2009
  33. Alex RiesenAug 5, 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.