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>();
}
Mutex::Lock _l(_lock);
SharedPtr<Peer> &ap = _peers[zta];
if (ap) {
ap->use(RR->node->now());
return ap;
{
Mutex::Lock _l(_lock);
const SharedPtr<Peer> *const ap = _peers.get(zta);
if (ap) {
(*ap)->use(RR->node->now());
return *ap;
}
}
Identity id(_getIdentity(zta));
if (id) {
try {
ap = SharedPtr<Peer>(new Peer(RR->identity,id));
ap->use(RR->node->now());
return ap;
} catch ( ... ) {} // invalid identity?
}
try {
Identity id(_getIdentity(zta));
if (id) {
SharedPtr<Peer> np(new Peer(RR->identity,id));
{
Mutex::Lock _l(_lock);
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>();
}

View File

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