From 9db9d6215f48167c0d1f441527796a14bc223ff7 Mon Sep 17 00:00:00 2001 From: gnuser Date: Sat, 2 Sep 2017 22:41:25 -0700 Subject: [PATCH] Refactor: make the read function simpler --- src/streams.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/streams.h b/src/streams.h index 8dc5a19ead..df1cd50c91 100644 --- a/src/streams.h +++ b/src/streams.h @@ -345,18 +345,16 @@ public: // Read from the beginning of the buffer unsigned int nReadPosNext = nReadPos + nSize; - if (nReadPosNext >= vch.size()) + if (nReadPosNext > vch.size()) { + throw std::ios_base::failure("CDataStream::read(): end of data"); + } + memcpy(pch, &vch[nReadPos], nSize); + if (nReadPosNext == vch.size()) { - if (nReadPosNext > vch.size()) - { - throw std::ios_base::failure("CDataStream::read(): end of data"); - } - memcpy(pch, &vch[nReadPos], nSize); nReadPos = 0; vch.clear(); return; } - memcpy(pch, &vch[nReadPos], nSize); nReadPos = nReadPosNext; }