Skip to content

Commit 07e3d1b

Browse files
TsynkPavelbaylesj
andauthored
Fix deallocate for working on old compiers (#1478)
Co-authored-by: Jordan Bayles <[email protected]>
1 parent 871f0cc commit 07e3d1b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

CMakeLists.txt

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

96+
include(CheckFunctionExists)
97+
check_function_exists(memset_s HAVE_MEMSET_S)
98+
if(HAVE_MEMSET_S)
99+
add_definitions("-DHAVE_MEMSET_S=1")
100+
endif()
101+
96102
if(JSONCPP_USE_SECURE_MEMORY)
97103
add_definitions("-DJSONCPP_USE_SECURE_MEMORY=1")
98104
endif()

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)