Fixed crashing problems: min and max had their logic inverted and Bytestream::read_string moved the cursor too early

Fri, 24 Jul 2015 00:43:16 +0300

author
Teemu Piippo <tsapii@utu.fi>
date
Fri, 24 Jul 2015 00:43:16 +0300
changeset 99
f9f73eeba3b7
parent 98
4f0f0b1b8e0b
child 100
d301ead29d7c

Fixed crashing problems: min and max had their logic inverted and Bytestream::read_string moved the cursor too early

sources/basics.h file | annotate | diff | comparison | revisions
sources/network/bytestream.cpp file | annotate | diff | comparison | revisions
--- a/sources/basics.h	Thu Jul 23 18:26:30 2015 +0300
+++ b/sources/basics.h	Fri Jul 24 00:43:16 2015 +0300
@@ -73,13 +73,13 @@
 template<typename T>
 T min (T a, T b)
 {
-	return (a < b) ? b : a;
+	return (a < b) ? a : b;
 }
 
 template<typename T>
 T max (T a, T b)
 {
-	return (a > b) ? b : a;
+	return (a > b) ? a : b;
 }
 
 template<typename T>
--- a/sources/network/bytestream.cpp	Thu Jul 23 18:26:30 2015 +0300
+++ b/sources/network/bytestream.cpp	Fri Jul 24 00:43:16 2015 +0300
@@ -192,22 +192,18 @@
 	unsigned char* stringBegin = m_cursor;
 	unsigned char* end = m_data + allocated_size();
 
-	// where's the end of the string?
+	// Where's the end of the string?
 	for (stringEnd = m_cursor; *stringEnd != '\0'; ++stringEnd)
 	{
 		if (stringEnd == end)
-			// past the end of the buffer! Argh!
-			throw IOError ("unterminated string in packet");
+		{
+			// Past the end of the buffer
+			throw IOError ("unterminated or too long string in packet");
+		}
 	}
 
+	unsigned int length = stringEnd - m_cursor;
 	m_cursor = stringEnd + 1;
-	unsigned int length = stringEnd - m_cursor;
-
-	// ensure we won't write past the buffer (note: we still moved
-	// past the excess bytes in the above statement, those are ignored)
-	if (length >= MAX_NETWORK_STRING)
-		length = MAX_NETWORK_STRING - 1;
-
 	memcpy (buffer, stringBegin, length);
 	buffer[length] = '\0';
 	return String (buffer);

mercurial