Skip to content

Commit

Permalink
Fix Coverity warnings
Browse files Browse the repository at this point in the history
Checking a ptr against null after dereferencing it.

Allowing exception throw to escape a noexcept function.

Both harmless.
  • Loading branch information
randombit committed Mar 7, 2019
1 parent 841c9cf commit 9252df3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
46 changes: 33 additions & 13 deletions src/lib/utils/mem_pool/mem_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,29 +364,49 @@ bool Memory_Pool::deallocate(void* p, size_t len) noexcept

if(n_bucket != 0)
{
lock_guard_type<mutex_type> lock(m_mutex);
try
{
lock_guard_type<mutex_type> lock(m_mutex);

std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
std::deque<Bucket>& buckets = m_buckets_for[n_bucket];

for(size_t i = 0; i != buckets.size(); ++i)
{
Bucket& bucket = buckets[i];
if(bucket.free(p))
for(size_t i = 0; i != buckets.size(); ++i)
{
if(bucket.empty())
Bucket& bucket = buckets[i];
if(bucket.free(p))
{
if(bucket.empty())
{
#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
OS::page_prohibit_access(bucket.ptr());
OS::page_prohibit_access(bucket.ptr());
#endif
m_free_pages.push_back(bucket.ptr());
m_free_pages.push_back(bucket.ptr());

if(i != buckets.size() - 1)
std::swap(buckets.back(), buckets[i]);
buckets.pop_back();
if(i != buckets.size() - 1)
std::swap(buckets.back(), buckets[i]);
buckets.pop_back();
}
return true;
}
return true;
}
}
catch(...)
{
/*
* The only exception throws that can occur in the above code are from
* either the STL or BOTAN_ASSERT failures. In either case, such an
* error indicates a logic error or data corruption in the memory
* allocator such that it is no longer safe to continue executing.
*
* Since this function is noexcept, simply letting the exception escape
* is sufficient for terminate to be called. However in this scenario
* it is implementation defined if any stack unwinding is performed.
* Since stack unwinding could cause further memory deallocations this
* could result in further corruption in this allocator state. To prevent
* this, call terminate directly.
*/
std::terminate();
}
}

return false;
Expand Down
12 changes: 5 additions & 7 deletions src/lib/utils/os_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,12 @@ std::vector<void*> OS::allocate_locked_pages(size_t count)
}
#endif

if(ptr != nullptr)
{
// Make guard page following the data page
page_prohibit_access(static_cast<uint8_t*>(ptr) + page_size);
std::memset(ptr, 0, 2*page_size); // zero both data and guard pages

std::memset(ptr, 0, page_size);
result.push_back(ptr);
}
// Make guard page following the data page
page_prohibit_access(static_cast<uint8_t*>(ptr) + page_size);

result.push_back(ptr);
}

return result;
Expand Down

0 comments on commit 9252df3

Please sign in to comment.