From 1c7a0d03a1ba0fa1ce4362694cf4fe5bf68f276d Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 14 Feb 2015 18:48:33 +0100 Subject: [PATCH] Fix a case where NetString::ReadStringFromStream might incorrectly return StatusEof refs #6109 --- lib/base/dynamicobject.cpp | 10 +++++++++- lib/base/netstring.cpp | 10 +++++++--- lib/remote/apilistener.cpp | 7 ++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/base/dynamicobject.cpp b/lib/base/dynamicobject.cpp index 1d0dda433..83278e676 100644 --- a/lib/base/dynamicobject.cpp +++ b/lib/base/dynamicobject.cpp @@ -315,7 +315,15 @@ void DynamicObject::RestoreObjects(const String& filename, int attributeTypes) String message; StreamReadContext src; - while (NetString::ReadStringFromStream(sfp, &message, src) == StatusNewItem) { + for (;;) { + StreamReadStatus srs = NetString::ReadStringFromStream(sfp, &message, src); + + if (srs == StatusEof) + break; + + if (srs != StatusNewItem) + continue; + upq.Enqueue(boost::bind(&DynamicObject::RestoreObject, message, attributeTypes)); restored++; } diff --git a/lib/base/netstring.cpp b/lib/base/netstring.cpp index 46c4041a2..3692c56da 100644 --- a/lib/base/netstring.cpp +++ b/lib/base/netstring.cpp @@ -37,9 +37,13 @@ StreamReadStatus NetString::ReadStringFromStream(const Stream::Ptr& stream, Stri if (context.Eof) return StatusEof; - if (context.MustRead && !context.FillFromStream(stream)) { - context.Eof = true; - return StatusEof; + if (context.MustRead) { + if (!context.FillFromStream(stream)) { + context.Eof = true; + return StatusEof; + } + + context.MustRead = false; } size_t header_length = 0; diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp index 7119af185..3fbdb30d5 100644 --- a/lib/remote/apilistener.cpp +++ b/lib/remote/apilistener.cpp @@ -691,9 +691,14 @@ void ApiListener::ReplayLog(const ApiClient::Ptr& client) Dictionary::Ptr pmessage; try { - if (NetString::ReadStringFromStream(logStream, &message, src) != StatusNewItem) + StreamReadStatus srs = NetString::ReadStringFromStream(logStream, &message, src); + + if (srs == StatusEof) break; + if (srs != StatusNewItem) + continue; + pmessage = JsonDecode(message); } catch (const std::exception&) { Log(LogWarning, "ApiListener") -- 2.40.0