//
// Don't eat heavily before inspecting this code.
//
+#define __STDC_WANT_LIB_EXT1__ 1
+#include <string.h>
+
#include <security_utilities/alloc.h>
#include <security_utilities/memutils.h>
#include <security_utilities/globalizer.h>
#include <stdlib.h>
#include <errno.h>
-using LowLevelMemoryUtilities::alignof;
+using LowLevelMemoryUtilities::alignof_template;
using LowLevelMemoryUtilities::increment;
using LowLevelMemoryUtilities::alignUp;
//
// Features of the Allocator root class
//
-bool Allocator::operator == (const Allocator &alloc) const throw()
+bool Allocator::operator == (const Allocator &alloc) const _NOEXCEPT
{
return this == &alloc;
}
// pool). This is trivially achieved here by using singletons.
//
struct DefaultAllocator : public Allocator {
- void *malloc(size_t size) throw(std::bad_alloc);
- void free(void *addr) throw();
- void *realloc(void *addr, size_t size) throw(std::bad_alloc);
+ void *malloc(size_t size);
+ void free(void *addr) _NOEXCEPT;
+ void *realloc(void *addr, size_t size);
};
struct SensitiveAllocator : public DefaultAllocator {
- void free(void *addr) throw();
- void *realloc(void *addr, size_t size) throw(std::bad_alloc);
+ void free(void *addr) _NOEXCEPT;
+ void *realloc(void *addr, size_t size);
};
struct DefaultAllocators {
}
}
-void *DefaultAllocator::malloc(size_t size) throw(std::bad_alloc)
+void *DefaultAllocator::malloc(size_t size)
{
if (void *result = ::malloc(size))
return result;
throw std::bad_alloc();
}
-void DefaultAllocator::free(void *addr) throw()
+void DefaultAllocator::free(void *addr) _NOEXCEPT
{
::free(addr);
}
-void *DefaultAllocator::realloc(void *addr, size_t newSize) throw(std::bad_alloc)
+void *DefaultAllocator::realloc(void *addr, size_t newSize)
{
if (void *result = ::realloc(addr, newSize))
return result;
throw std::bad_alloc();
}
-void SensitiveAllocator::free(void *addr) throw()
+void SensitiveAllocator::free(void *addr) _NOEXCEPT
{
- memset(addr, 0, malloc_size(addr));
+ size_t size = malloc_size(addr);
+ ::memset_s(addr, size, 0, size);
DefaultAllocator::free(addr);
}
-void *SensitiveAllocator::realloc(void *addr, size_t newSize) throw(std::bad_alloc)
+void *SensitiveAllocator::realloc(void *addr, size_t newSize)
{
size_t oldSize = malloc_size(addr);
if (newSize < oldSize)
- memset(increment(addr, newSize), 0, oldSize - newSize);
+ ::memset_s(increment(addr, newSize), oldSize - newSize, 0, oldSize - newSize);
return DefaultAllocator::realloc(addr, newSize);
}
// functions to safely free our (hidden) pointer without knowing about it.
// An allocator argument of NULL is interpreted as the standard allocator.
//
-void *CssmHeap::operator new (size_t size, Allocator *alloc) throw(std::bad_alloc)
+void *CssmHeap::operator new (size_t size, Allocator *alloc)
{
- if (alloc == NULL)
+ if (size > SIZE_T_MAX / 2) {
+ throw std::bad_alloc();
+ }
+ if (alloc == NULL) {
alloc = &Allocator::standard();
- size = alignUp(size, alignof<Allocator *>());
+ }
+ size = alignUp(size, alignof_template<Allocator *>());
size_t totalSize = size + sizeof(Allocator *);
void *addr = alloc->malloc(totalSize);
*(Allocator **)increment(addr, size) = alloc;
return addr;
}
-void CssmHeap::operator delete (void *addr, size_t size, Allocator *alloc) throw()
+void CssmHeap::operator delete (void *addr, size_t size, Allocator *alloc) _NOEXCEPT
{
alloc->free(addr); // as per C++ std, called (only) if construction fails
}
-void CssmHeap::operator delete (void *addr, size_t size) throw()
+void CssmHeap::operator delete (void *addr, size_t size) _NOEXCEPT
{
- void *end = increment(addr, alignUp(size, alignof<Allocator *>()));
+ void *end = increment(addr, alignUp(size, alignof_template<Allocator *>()));
(*(Allocator **)end)->free(addr);
}