From 860d265a878ca444fca358f468a32df32a7f4444 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sat, 27 Apr 2013 13:29:14 +0200
Subject: Zero memory more securely in SafeByteArray.

Change-Id: I0d9db512e0c525d87fddc26dc73ea95d2b54b54d

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:
 	};
 }
-- 
cgit v0.10.2-6-g49f6