From 5cfc34ebc292c82f7ec7ce19e36bf0774986b270 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Mon, 26 Jun 2006 21:32:52 +0000 Subject: Fixed the readString method (synchronized with the server version by Guillaume Melquiond). --- src/net/messagein.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'src/net') diff --git a/src/net/messagein.cpp b/src/net/messagein.cpp index 9abe4a2d..bbc0a44c 100644 --- a/src/net/messagein.cpp +++ b/src/net/messagein.cpp @@ -155,28 +155,24 @@ MessageIn::skip(unsigned int length) std::string MessageIn::readString(int length) { - int stringLength = 0; - // Get string length if (length < 0) { - stringLength = readInt16(); - } else { - stringLength = length; + length = readInt16(); } - // Make sure there is enough data available - assert(mPos + length <= mLength); + // Make sure the string isn't erroneous + if (length < 0 || mPos + length > mLength) { + mPos = mLength + 1; + return ""; + } // Read the string - char *tmpString = new char[stringLength + 1]; - memcpy(tmpString, (void*)&mData[mPos], stringLength); - tmpString[stringLength] = 0; - mPos += stringLength; - - std::string read = tmpString; - delete[] tmpString; - - return read; + char const *stringBeg = mData + mPos; + char const *stringEnd = (char const *)memchr(stringBeg, '\0', length); + std::string readString(stringBeg, + stringEnd ? stringEnd - stringBeg : length); + mPos += length; + return readString; } Sint8& operator<<(Sint8 &lhs, MessageIn &msg) -- cgit v1.2.3-70-g09d2