I don't have a whole redesign of the function in mind, but I thought about it some more and now I have a more elegant patch that I think fixes the problem. I made the second `++i` unconditional because I think `i` is now guaranteed to be less than MaxBytes at that point: diff --git a/src/plain.cc b/src/plain.cc index 0f5d6a48..0770af92 100644 --- a/src/plain.cc +++ b/src/plain.cc @@ -176,6 +176,14 @@ void DilloPlain::write(void *Buf, uint_t BufSize, int Eof) _MSG("DilloPlain::write Eof=%d\n", Eof); Start = (char*)Buf + Start_Ofs; + + if (Start_Ofs > 0 && Start_Ofs < BufSize && + Start[-1] == '\r' && Start[0] == '\n'){ + // Prevent \r\n from resulting in 2 line breaks. + ++Start_Ofs; + ++Start; + } + MaxBytes = BufSize - Start_Ofs; i = len = 0; while ( i < MaxBytes ) { @@ -189,8 +197,8 @@ void DilloPlain::write(void *Buf, uint_t BufSize, int Eof) break; case ST_Eol: addLine(Start + i - len, len); - if (Start[i] == '\r' && Start[i + 1] == '\n') ++i; - if (i < MaxBytes) ++i; + if (Start[i] == '\r' && i + 1 < MaxBytes && Start[i + 1] == '\n') ++i; + ++i; state = ST_SeekingEol; len = 0; break;