From 7d1fc7b9e3f5fba4c1f0d09f841a08ca94dd5e8f Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 17 Jul 2003 02:34:48 +0000 Subject: [PATCH] Better \n tolerance. svn path=/trunk/; revision=3818 --- NEWS | 5 +++- transact.c | 72 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/NEWS b/NEWS index 33d6d0b2..38d0944b 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,9 @@ Fixes Debian bug #184469. * Benjamin Drieu's patch to enable auth=cram-md5. Fixes Debian bug #185232. +* Sunil Shetye's configure.in patch to avoid spurious search order messages + from GCC. +* Header-reading code now copes better with lines ending in \n only. fetchmail-6.2.2 (Fri Feb 28 21:34:26 EST 2003), 22345 lines: @@ -54,7 +57,7 @@ fetchmail-6.1.3 (Thu Nov 28 05:35:15 EST 2002), 22203 lines: * Fix logout-after-idle-delivery bug (Sunil Shetye). * Sunil Shetye's patch to bulletproof end-of-header detection. * Sunil's fix for the STARTTLS problem -- repoll if TLS nabdshake - fails. The attenmpt to set up STARTTLS can be suppressed with 'sslproto ""'. + fails. The attempt to set up STARTTLS can be suppressed with 'sslproto ""'. There are 540 people on fetchmail-friends and 701 on fetchmail-announce. diff --git a/transact.c b/transact.c index cee17652..0b16e5c2 100644 --- a/transact.c +++ b/transact.c @@ -417,7 +417,7 @@ int readheaders(int sock, skipcount = 0; ctl->mimemsg = 0; - for (remaining = fetchlen; remaining > 0 || protocol->delimited; remaining -= linelen) + for (remaining = fetchlen; remaining > 0 || protocol->delimited; ) { char *line; int overlong = FALSE; @@ -435,6 +435,8 @@ int readheaders(int sock, return(PS_SOCKET); } set_timeout(0); + + remaining -= n; linelen += n; msgblk.msglen += n; @@ -443,47 +445,33 @@ int readheaders(int sock, * line exceeds MSGBUFSIZE. */ if ( n && buf[n-1] != '\n' ) { - unsigned int llen = strlen(line); overlong = TRUE; - line = realloc(line, llen + n + 1); - strcpy(line + llen, buf); + line = realloc(line, linelen); + memcpy(line + linelen - n, buf, n); ch = ' '; /* So the next iteration starts */ continue; } + /* lines may not be properly CRLF terminated; fix this for qmail */ - if (ctl->forcecr) + /* we don't want to overflow the buffer here */ + if (ctl->forcecr && buf[n-1] == '\n' && (n == 1 || buf[n-2] != '\r')) { - cp = buf + strlen(buf) - 1; - if (*cp == '\n' && (cp == buf || cp[-1] != '\r')) - { - *cp++ = '\r'; - *cp++ = '\n'; - *cp++ = '\0'; - } + char * tcp; + line = (char *) realloc(line, linelen + 2); + memcpy(line + linelen - n, buf, n - 1); + tcp = line + linelen - 1; + *tcp++ = '\r'; + *tcp++ = '\n'; + *tcp++ = '\0'; + n++; + linelen++; + } + else + { + line = (char *) realloc(line, linelen + 1); + memcpy(line + linelen - n, buf, n + 1); } - - /* - * Decode MIME encoded headers. We MUST do this before - * looking at the Content-Type / Content-Transfer-Encoding - * headers (RFC 2046). - */ - if ( ctl->mimedecode && overlong ) { - /* - * If we received an overlong line, we have to decode the - * whole line at once. - */ - line = (char *) realloc(line, strlen(line) + strlen(buf) +1); - strcat(line, buf); - UnMimeHeader(line); - } - else { - if ( ctl->mimedecode ) - UnMimeHeader(buf); - - line = (char *) realloc(line, strlen(line) + strlen(buf) +1); - strcat(line, buf); - } /* check for end of headers */ if (end_of_header(line)) @@ -545,6 +533,22 @@ int readheaders(int sock, sizeticker -= SIZETICKER; } } + /* + * Decode MIME encoded headers. We MUST do this before + * looking at the Content-Type / Content-Transfer-Encoding + * headers (RFC 2046). + */ + if ( ctl->mimedecode ) + { + char *tcp; + UnMimeHeader(line); + /* the line is now shorter. So we retrace back till we find our terminating + * combination \n\0, we move backwards to make sure that we don't catch som + * \n\0 stored in the decoded part of the message */ + for(tcp = line + linelen - 1; tcp > line && (*tcp != 0 || tcp[-1] != '\n'); tcp--); + if(tcp > line) linelen = tcp - line; + } + /* we see an ordinary (non-header, non-message-delimiter line */ has_nuls = (linelen != strlen(line)); -- 2.43.2