Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization/Deserialization Problem. #3

Open
chaosape opened this issue Jun 9, 2017 · 0 comments
Open

Serialization/Deserialization Problem. #3

chaosape opened this issue Jun 9, 2017 · 0 comments

Comments

@chaosape
Copy link
Collaborator

chaosape commented Jun 9, 2017

Consider the serialization and deserialization code generated by LmcpGen:

   void MissionCommand::pack(avtas::lmcp::ByteBuffer & buf) const
   {
      // Call parent's pack method
      afrl::cmasi::VehicleActionCommand::pack(buf);
      // Copy the class into the buffer
      buf.putUShort( static_cast<uint16_t>(__WaypointList.size()));
      for (size_t i=0; i<__WaypointList.size(); i++)
      {
         avtas::lmcp::Factory::putObject( (avtas::lmcp::Object*) __WaypointList[i], buf);
      }
      buf.putLong(__FirstWaypoint);

   }
   
   void MissionCommand::unpack(avtas::lmcp::ByteBuffer & buf)
   {
      // Call parent's unpack method
      afrl::cmasi::VehicleActionCommand::unpack(buf);
      // Copy the buffer into the class
      for (size_t i=0; i<__WaypointList.size(); i++)
      {
         if (__WaypointList[i] != nullptr)
            delete __WaypointList[i];
      }
      __WaypointList.clear();
      uint16_t __WaypointList_length = buf.getUShort();
      for (uint32_t i=0; i< __WaypointList_length; i++)
      {
         if (buf.getBool())
         {
            int64_t series_id = buf.getLong();
            uint32_t msgtype = buf.getUInt();
            uint16_t version = buf.getUShort();
            afrl::cmasi::Waypoint* e = (afrl::cmasi::Waypoint*) avtas::lmcp::Factory::createObject( series_id, msgtype, version );
            if ( e != nullptr) e->unpack(buf); 
            __WaypointList.push_back(e);
         }
      }
      __FirstWaypoint = buf.getLong();
   }

Suppose our waypoint vector has 65537 waypoints. During serialization, the pack method will write a short indicating that there is only 1 waypoint but may go one to write many more waypoints than this. size_t's definition is something that is architecture dependent and is often, though not always, identical to the word size.

Now consider what can happen when deserializing such data when sizeof(size_t)>16, we will read the short indicating there is only 1 waypoint, read 1 waypoint, then read an int64_t that we will interpret as the starting waypoint id. However, this starting waypoint will, in actuality, be the first 64 bits of the next waypoint. I think this will actually correspond to the latitude stored as a double. Regardless, this seems like behavior one should try to avoid.

lhumphrey pushed a commit that referenced this issue Aug 28, 2020
This material was migrated into src a while ago and is now out of date.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant