diff options
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Base/SConscript | 1 | ||||
-rw-r--r-- | Swiften/Base/SafeAllocator.cpp | 27 | ||||
-rw-r--r-- | Swiften/Base/SafeAllocator.h | 8 |
3 files changed, 34 insertions, 2 deletions
diff --git a/Swiften/Base/SConscript b/Swiften/Base/SConscript index 754164b..b56db8c 100644 --- a/Swiften/Base/SConscript +++ b/Swiften/Base/SConscript @@ -4,6 +4,7 @@ objects = swiften_env.SwiftenObject([ "ByteArray.cpp", "DateTime.cpp", "SafeByteArray.cpp", + "SafeAllocator.cpp", "Error.cpp", "Log.cpp", "Paths.cpp", diff --git a/Swiften/Base/SafeAllocator.cpp b/Swiften/Base/SafeAllocator.cpp new file mode 100644 index 0000000..d61d8b9 --- /dev/null +++ b/Swiften/Base/SafeAllocator.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2013 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <Swiften/Base/SafeByteArray.h> + +#include <Swiften/Base/Platform.h> +#ifdef SWIFTEN_PLATFORM_WINDOWS +#include <windows.h> +#endif + +namespace Swift { + +void secureZeroMemory(char* memory, size_t numberOfBytes) { +#ifdef SWIFTEN_PLATFORM_WINDOWS + SecureZeroMemory(memory, numberOfBytes); +#else + volatile char* p = memory; + for (size_t i = 0; i < numberOfBytes; ++i) { + *(p++) = 0; + } +#endif +} + +} diff --git a/Swiften/Base/SafeAllocator.h b/Swiften/Base/SafeAllocator.h index f59119e..b01d77d 100644 --- a/Swiften/Base/SafeAllocator.h +++ b/Swiften/Base/SafeAllocator.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011 Remko Tronçon + * Copyright (c) 2011-2013 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ @@ -10,6 +10,8 @@ #include <algorithm> namespace Swift { + void secureZeroMemory(char* memory, size_t numberOfBytes); + template<typename T> class SafeAllocator : public std::allocator<T> { public: @@ -23,8 +25,10 @@ namespace Swift { ~SafeAllocator() throw() {} void deallocate (T* p, size_t num) { - std::fill(reinterpret_cast<char*>(p), reinterpret_cast<char*>(p + num), 0); + secureZeroMemory(reinterpret_cast<char*>(p), num); std::allocator<T>::deallocate(p, num); } + + private: }; } |