From 02d1f1cc579d9d2f395ba3e10309d17a636b797a Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 20 Jun 2018 09:59:18 +0200 Subject: [PATCH 1/2] Stream#ReadLine(): fix false positive buffer underflow indicator refs #6354 --- lib/base/stream.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/base/stream.cpp b/lib/base/stream.cpp index 3048f096c..a7ac2067c 100644 --- a/lib/base/stream.cpp +++ b/lib/base/stream.cpp @@ -143,7 +143,16 @@ StreamReadStatus Stream::ReadLine(String *line, StreamReadContext& context, bool } } - context.MustRead = (count <= 1); + switch (count) { + case 0: + context.MustRead = true; + break; + case 1: + context.MustRead = first_newline == (context.Size - 1u); + break; + default: + context.MustRead = false; + } if (count > 0) { *line = String(context.Buffer, &(context.Buffer[first_newline])); From 1c213877a4554bbe3632eada2a6fd4a99f8b90de Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 20 Jun 2018 17:28:52 +0200 Subject: [PATCH 2/2] Stream#ReadLine(): simplify algorithm refs #6354 --- lib/base/stream.cpp | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/lib/base/stream.cpp b/lib/base/stream.cpp index a7ac2067c..0b847060f 100644 --- a/lib/base/stream.cpp +++ b/lib/base/stream.cpp @@ -129,40 +129,19 @@ StreamReadStatus Stream::ReadLine(String *line, StreamReadContext& context, bool } } - int count = 0; - size_t first_newline; - for (size_t i = 0; i < context.Size; i++) { if (context.Buffer[i] == '\n') { - count++; + *line = String(context.Buffer, context.Buffer + i); + boost::algorithm::trim_right(*line); - if (count == 1) - first_newline = i; - else if (count > 1) - break; + context.DropData(i + 1u); + + context.MustRead = !context.Size; + return StatusNewItem; } } - switch (count) { - case 0: - context.MustRead = true; - break; - case 1: - context.MustRead = first_newline == (context.Size - 1u); - break; - default: - context.MustRead = false; - } - - if (count > 0) { - *line = String(context.Buffer, &(context.Buffer[first_newline])); - boost::algorithm::trim_right(*line); - - context.DropData(first_newline + 1); - - return StatusNewItem; - } - + context.MustRead = true; return StatusNeedData; }