Skip to content

Commit 2636159

Browse files
committed
Fix deallocate for working on old compiers
1 parent 69098a1 commit 2636159

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Diff for: CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
103103
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "Executable/dll output dir.")
104104
endif()
105105

106+
include(CheckFunctionExists)
107+
check_function_exists(memset_s HAVE_MEMSET_S)
108+
if(HAVE_MEMSET_S)
109+
add_definitions("-DHAVE_MEMSET_S=1")
110+
endif()
111+
106112
set(JSONCPP_USE_SECURE_MEMORY "0" CACHE STRING "-D...=1 to use memory-wiping allocator for STL")
107113

108114
configure_file("${PROJECT_SOURCE_DIR}/version.in"

Diff for: include/json/allocator.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef JSON_ALLOCATOR_H_INCLUDED
77
#define JSON_ALLOCATOR_H_INCLUDED
88

9+
#include <algorithm>
910
#include <cstring>
1011
#include <memory>
1112

@@ -38,8 +39,16 @@ template <typename T> class SecureAllocator {
3839
* The memory block is filled with zeroes before being released.
3940
*/
4041
void deallocate(pointer p, size_type n) {
41-
// memset_s is used because memset may be optimized away by the compiler
42+
// These constructs will not be removed by the compiler during optimization,
43+
// unlike memset.
44+
#if defined(HAVE_MEMSET_S)
4245
memset_s(p, n * sizeof(T), 0, n * sizeof(T));
46+
#elif defined(_WIN32)
47+
RtlSecureZeroMemory(p, n * sizeof(T));
48+
#else
49+
std::fill_n(reinterpret_cast<volatile unsigned char*>(p), n, 0);
50+
#endif
51+
4352
// free using "global operator delete"
4453
::operator delete(p);
4554
}

0 commit comments

Comments
 (0)