diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-06-26 21:32:52 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-06-26 21:32:52 +0000 |
commit | 5cfc34ebc292c82f7ec7ce19e36bf0774986b270 (patch) | |
tree | f329fd18eeab2ffa2563b09e8512d7a5ba909f2b /src/net | |
parent | 579ccd64cc098f4caa4ba1ebc6918488788b2a57 (diff) | |
download | mana-5cfc34ebc292c82f7ec7ce19e36bf0774986b270.tar.gz mana-5cfc34ebc292c82f7ec7ce19e36bf0774986b270.tar.bz2 mana-5cfc34ebc292c82f7ec7ce19e36bf0774986b270.tar.xz mana-5cfc34ebc292c82f7ec7ce19e36bf0774986b270.zip |
Fixed the readString method (synchronized with the server version by Guillaume
Melquiond).
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/messagein.cpp | 28 |
1 files changed, 12 insertions, 16 deletions
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) |