Remove raw message output on AOM deserialization failure
authorkwolekr <kwolekr@minetest.net>
Tue, 14 Jul 2015 03:29:29 +0000 (23:29 -0400)
committerkwolekr <kwolekr@minetest.net>
Tue, 14 Jul 2015 03:29:29 +0000 (23:29 -0400)
Improve TOCLIENT_ACTIVE_OBJECT_MESSAGES robustness for handling invalid data

src/environment.cpp
src/network/clientpackethandler.cpp

index 09db886a83d6c463bc67d8c5a5ef3eb6d7e8b3e8..5affda6fad71427d792b32b1751be051a70e570c 100644 (file)
@@ -2535,28 +2535,23 @@ void ClientEnvironment::removeActiveObject(u16 id)
        m_active_objects.erase(id);
 }
 
-void ClientEnvironment::processActiveObjectMessage(u16 id,
-               const std::string &data)
+void ClientEnvironment::processActiveObjectMessage(u16 id, const std::string &data)
 {
-       ClientActiveObject* obj = getActiveObject(id);
-       if(obj == NULL)
-       {
-               infostream<<"ClientEnvironment::processActiveObjectMessage():"
-                               <<" got message for id="<<id<<", which doesn't exist."
-                               <<std::endl;
+       ClientActiveObject *obj = getActiveObject(id);
+       if (obj == NULL) {
+               infostream << "ClientEnvironment::processActiveObjectMessage():"
+                       << " got message for id=" << id << ", which doesn't exist."
+                       << std::endl;
                return;
        }
-       try
-       {
+
+       try {
                obj->processMessage(data);
-       }
-       catch(SerializationError &e)
-       {
+       } catch (SerializationError &e) {
                errorstream<<"ClientEnvironment::processActiveObjectMessage():"
-                               <<" id="<<id<<" type="<<obj->getType()
-                               <<" SerializationError in processMessage(),"
-                               <<" message="<<serializeJsonString(data)
-                               <<std::endl;
+                       << " id=" << id << " type=" << obj->getType()
+                       << " SerializationError in processMessage(): " << e.what()
+                       << std::endl;
        }
 }
 
index 4cebb2184414915357ce064654c85b6eda41e546..15d5456fac11e123faeb476a1bd079199d58c451 100644 (file)
@@ -449,33 +449,23 @@ void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt)
                        string message
                }
        */
-       char buf[6];
-       // Get all data except the command number
        std::string datastring(pkt->getString(0), pkt->getSize());
-       // Throw them in an istringstream
        std::istringstream is(datastring, std::ios_base::binary);
 
        try {
-               while(is.eof() == false) {
-                       is.read(buf, 2);
-                       u16 id = readU16((u8*)buf);
-                       if (is.eof())
+               while (is.good()) {
+                       u16 id = readU16(is);
+                       if (!is.good())
                                break;
-                       is.read(buf, 2);
-                       size_t message_size = readU16((u8*)buf);
-                       std::string message;
-                       message.reserve(message_size);
-                       for (u32 i = 0; i < message_size; i++) {
-                               is.read(buf, 1);
-                               message.append(buf, 1);
-                       }
+
+                       std::string message = deSerializeString(is);
+
                        // Pass on to the environment
                        m_env.processActiveObjectMessage(id, message);
                }
-       // Packet could be unreliable then ignore it
-       } catch (PacketError &e) {
-               infostream << "handleCommand_ActiveObjectMessages: " << e.what()
-                                       << ". The packet is unreliable, ignoring" << std::endl;
+       } catch (SerializationError &e) {
+               errorstream << "Client::handleCommand_ActiveObjectMessages: "
+                       << "caught SerializationError: " << e.what() << std::endl;
        }
 }