diff --git a/node/Peer.hpp b/node/Peer.hpp index 043519d4d..7e7e7f465 100644 --- a/node/Peer.hpp +++ b/node/Peer.hpp @@ -470,7 +470,7 @@ public: b.append((uint16_t)_vRevision); b.append((uint32_t)_latency); - b.append((uint32_t)_numPaths); + b.append((uint16_t)_numPaths); for(unsigned int i=0;i<_numPaths;++i) _paths[i].serialize(b); @@ -497,7 +497,7 @@ public: } } - b.setAt(recSizePos,(uint32_t)((b.size() - 4) - recSizePos)); // set size + b.template setAt(recSizePos,(uint32_t)(b.size() - (recSizePos + 4))); // set size } /** @@ -511,7 +511,7 @@ public: template static inline SharedPtr deserializeNew(const Identity &myIdentity,const Buffer &b,unsigned int &p) { - const uint32_t recSize = b.template at(p); p += 4; + const unsigned int recSize = b.template at(p); p += 4; if ((p + recSize) > b.size()) return SharedPtr(); // size invalid if (b.template at(p) != 1) @@ -540,7 +540,7 @@ public: np->_vRevision = b.template at(p); p += 2; np->_latency = b.template at(p); p += 4; - const unsigned int numPaths = b.template at(p); p += 4; + const unsigned int numPaths = b.template at(p); p += 2; for(unsigned int i=0;i_paths[np->_numPaths++].deserialize(b,p); diff --git a/node/Topology.cpp b/node/Topology.cpp index a35585582..6e8467c1a 100644 --- a/node/Topology.cpp +++ b/node/Topology.cpp @@ -47,31 +47,20 @@ Topology::Topology(const RuntimeEnvironment *renv) : unsigned int ptr = 0; while ((ptr + 4) < alls.size()) { - // Each Peer serializes itself prefixed by a record length (not including the size of the length itself) - unsigned int reclen = (unsigned int)all[ptr] & 0xff; - reclen <<= 8; - reclen |= (unsigned int)all[ptr + 1] & 0xff; - reclen <<= 8; - reclen |= (unsigned int)all[ptr + 2] & 0xff; - reclen <<= 8; - reclen |= (unsigned int)all[ptr + 3] & 0xff; - - if (((ptr + reclen) > alls.size())||(reclen > ZT_PEER_SUGGESTED_SERIALIZATION_BUFFER_SIZE)) - break; - try { + const unsigned int reclen = ( // each Peer serialized record is prefixed by a record length + ((((unsigned int)all[ptr]) & 0xff) << 24) | + ((((unsigned int)all[ptr + 1]) & 0xff) << 16) | + ((((unsigned int)all[ptr + 2]) & 0xff) << 8) | + (((unsigned int)all[ptr + 3]) & 0xff) + ); unsigned int pos = 0; - SharedPtr p(Peer::deserializeNew(RR->identity,Buffer(all + ptr,reclen),pos)); - if (pos != reclen) - break; + SharedPtr p(Peer::deserializeNew(RR->identity,Buffer(all + ptr,reclen + 4),pos)); ptr += pos; - if ((p)&&(p->address() != RR->identity.address())) { - _peers[p->address()] = p; - } else { + if (!p) break; // stop if invalid records - } - } catch (std::exception &exc) { - break; + if (p->address() != RR->identity.address()) + _peers[p->address()] = p; } catch ( ... ) { break; // stop if invalid records }