mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Better error checking in CachedViewImpl::read:
* Prevent null pointer dereferences * Alway check for correct sle type before returning sle * Reformat code
This commit is contained in:
@@ -31,38 +31,38 @@ CachedViewImpl::exists (Keylet const& k) const
|
||||
}
|
||||
|
||||
std::shared_ptr<SLE const>
|
||||
CachedViewImpl::read (Keylet const& k) const
|
||||
CachedViewImpl::read(Keylet const& k) const
|
||||
{
|
||||
{
|
||||
std::lock_guard<
|
||||
std::mutex> lock(mutex_);
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto const iter = map_.find(k.key);
|
||||
if (iter != map_.end())
|
||||
{
|
||||
if (! k.check(*iter->second))
|
||||
if (!iter->second || !k.check(*iter->second))
|
||||
return nullptr;
|
||||
return iter->second;
|
||||
}
|
||||
}
|
||||
auto const digest =
|
||||
base_.digest(k.key);
|
||||
if (! digest)
|
||||
auto const digest = base_.digest(k.key);
|
||||
if (!digest)
|
||||
return nullptr;
|
||||
auto sle = cache_.fetch(*digest,
|
||||
[&]() { return base_.read(k); });
|
||||
std::lock_guard<
|
||||
std::mutex> lock(mutex_);
|
||||
auto const iter =
|
||||
map_.find(k.key);
|
||||
if (iter == map_.end())
|
||||
auto sle = cache_.fetch(*digest, [&]() { return base_.read(k); });
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
auto const er = map_.emplace(k.key, sle);
|
||||
auto const& iter = er.first;
|
||||
bool const inserted = er.second;
|
||||
if (iter->second && !k.check(*iter->second))
|
||||
{
|
||||
map_.emplace(k.key, sle);
|
||||
return sle;
|
||||
if (!inserted)
|
||||
{
|
||||
// On entry, this function did not find this key in map_. Now something
|
||||
// (another thread?) has inserted the sle into the map and it has
|
||||
// the wrong type.
|
||||
LogicError("CachedView::read: wrong type");
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
if (! k.check(*iter->second))
|
||||
LogicError("CachedView::read: wrong type");
|
||||
return iter->second;
|
||||
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
Reference in New Issue
Block a user