getPeer() had a small potential to be unsafe.

This commit is contained in:
Adam Ierymenko 2015-10-30 13:39:28 -07:00
parent f974517f64
commit 377ccff600
2 changed files with 22 additions and 18 deletions

View file

@ -146,26 +146,30 @@ SharedPtr<Peer> Topology::getPeer(const Address &zta)
return SharedPtr<Peer>(); return SharedPtr<Peer>();
} }
Mutex::Lock _l(_lock); {
Mutex::Lock _l(_lock);
SharedPtr<Peer> &ap = _peers[zta]; const SharedPtr<Peer> *const ap = _peers.get(zta);
if (ap) {
if (ap) { (*ap)->use(RR->node->now());
ap->use(RR->node->now()); return *ap;
return ap; }
} }
Identity id(_getIdentity(zta)); try {
if (id) { Identity id(_getIdentity(zta));
try { if (id) {
ap = SharedPtr<Peer>(new Peer(RR->identity,id)); SharedPtr<Peer> np(new Peer(RR->identity,id));
ap->use(RR->node->now()); {
return ap; Mutex::Lock _l(_lock);
} catch ( ... ) {} // invalid identity? SharedPtr<Peer> &ap = _peers[zta];
} if (!ap)
ap.swap(np);
ap->use(RR->node->now());
return ap;
}
}
} catch ( ... ) {} // invalid identity on disk?
// If we get here it means we read an invalid cache identity or had some other error
_peers.erase(zta);
return SharedPtr<Peer>(); return SharedPtr<Peer>();
} }

View file

@ -87,7 +87,7 @@ public:
inline SharedPtr<Peer> getPeerNoCache(const Address &zta,const uint64_t now) inline SharedPtr<Peer> getPeerNoCache(const Address &zta,const uint64_t now)
{ {
Mutex::Lock _l(_lock); Mutex::Lock _l(_lock);
const SharedPtr<Peer> *ap = _peers.get(zta); const SharedPtr<Peer> *const ap = _peers.get(zta);
if (ap) { if (ap) {
(*ap)->use(now); (*ap)->use(now);
return *ap; return *ap;