Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 052e892

Browse files
committedApr 4, 2023
Fix deallocate for working on old compiers
1 parent 8190e06 commit 052e892

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed
 

‎CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib" CACHE PATH "Library
101101
set(CMAKE_PDB_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "PDB (MSVC debug symbol)output dir.")
102102
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" CACHE PATH "Executable/dll output dir.")
103103

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

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

‎include/json/allocator.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <cstring>
1010
#include <memory>
11+
#include <algorithm>
1112

1213
#pragma pack(push)
1314
#pragma pack()
@@ -38,8 +39,14 @@ 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+
#if defined(HAVE_MEMSET_S)
4243
memset_s(p, n * sizeof(T), 0, n * sizeof(T));
44+
#elif defined(_WIN32)
45+
RtlSecureZeroMemory(p, n * sizeof(T));
46+
#else
47+
std::fill_n(reinterpret_cast<volatile unsigned char*>(p), n, 0);
48+
#endif
49+
4350
// free using "global operator delete"
4451
::operator delete(p);
4552
}

0 commit comments

Comments
 (0)
Please sign in to comment.