From 9195afe8ddcce043591516a126646dc35b111a8d Mon Sep 17 00:00:00 2001 From: fluxide Date: Tue, 5 Aug 2025 02:37:08 +0300 Subject: [PATCH 1/2] dbstring adjustments on windows empty_buf function returns a char* now, and the free function in dbarray uses the get_size_ptr function to detect uninitialized void* objects --- CMakeLists.txt | 2 +- src/dbarray.cpp | 14 ++++++++------ src/lcf/dbarrayalloc.h | 5 ++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ba56ace..38f6a750 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ list(APPEND CMAKE_PREFIX_PATH "${LIBLCF_PREFIX_PATH_APPEND}") set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS ON) - +set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL) # lcf library files set(LCF_SOURCES src/dbarray.cpp diff --git a/src/dbarray.cpp b/src/dbarray.cpp index b10f4ba6..4edd91c8 100644 --- a/src/dbarray.cpp +++ b/src/dbarray.cpp @@ -12,8 +12,6 @@ namespace lcf { -const DBArrayAlloc::size_type DBArrayAlloc::_empty_buf[2] = { 0, 0 }; - static ptrdiff_t HeaderSize(size_t align) { return std::max(sizeof(DBArrayAlloc::size_type), align); } @@ -48,8 +46,9 @@ void* DBArrayAlloc::alloc(size_type size, size_type field_size, size_type align) void DBArrayAlloc::free(void* p, size_type align) noexcept { assert(p != nullptr); - if (p != empty_buf()) { - auto* raw = Adjust(p, -HeaderSize(align)); + if (*get_size_ptr(p)!=0) { + void* raw = new void*; + raw = Adjust(p, -HeaderSize(align)); #ifdef LCF_DEBUG_DBARRAY std::cout << "DBArray: Free" << " align=" << align @@ -58,7 +57,9 @@ void DBArrayAlloc::free(void* p, size_type align) noexcept { << " field_size=" << *get_size_ptr(p) << std::endl; #endif - ::operator delete(raw); + delete(raw); + + } } @@ -71,7 +72,7 @@ char* DBString::construct_z(const char* s, size_t len) { } char* DBString::construct_sv(const char* s, size_t len) { - auto* p = alloc(len); + char* p = alloc(len); if (len) { std::memcpy(p, s, len); p[len] = '\0'; @@ -80,6 +81,7 @@ char* DBString::construct_sv(const char* s, size_t len) { } DBString& DBString::operator=(const DBString& o) { + if (this != &o) { destroy(); _storage = construct_z(o.data(), o.size()); diff --git a/src/lcf/dbarrayalloc.h b/src/lcf/dbarrayalloc.h index 740c3c2b..085defbb 100644 --- a/src/lcf/dbarrayalloc.h +++ b/src/lcf/dbarrayalloc.h @@ -27,9 +27,8 @@ struct DBArrayAlloc { static void free(void* p, size_type align) noexcept; static void* empty_buf() { - return const_cast(&_empty_buf[1]); + return const_cast (&_empty_buf); } - static constexpr size_type* get_size_ptr(void* p) { return static_cast(p) - 1; } @@ -39,7 +38,7 @@ struct DBArrayAlloc { } private: - static const size_type _empty_buf[2]; + static const char _empty_buf = '\0'; }; } // namespace lcf From 11e31e7a26bcba6461420d937cf4174923007023 Mon Sep 17 00:00:00 2001 From: fluxide Date: Fri, 13 Feb 2026 18:07:30 +0300 Subject: [PATCH 2/2] dbstring adjustments on windows #2 -identified the problem with heap allocation in dbstrings -empty_buf() now returns 2 bytes, properly mimicking the void* structure of a dbstring. --- .gitignore | 2 +- CMakeLists.txt | 1 - src/lcf/dbarrayalloc.h | 5 +++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index fc831d1d..f3f6dba6 100644 --- a/.gitignore +++ b/.gitignore @@ -27,7 +27,7 @@ CMakeFiles/ CMakeDoxyfile.in install_manifest.txt Testing/ -build/ +build*/ # msvc / Windows *.vcxproj* diff --git a/CMakeLists.txt b/CMakeLists.txt index 38f6a750..d26a18c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,6 @@ list(APPEND CMAKE_PREFIX_PATH "${LIBLCF_PREFIX_PATH_APPEND}") set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS ON) -set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL) # lcf library files set(LCF_SOURCES src/dbarray.cpp diff --git a/src/lcf/dbarrayalloc.h b/src/lcf/dbarrayalloc.h index 085defbb..edfc1faa 100644 --- a/src/lcf/dbarrayalloc.h +++ b/src/lcf/dbarrayalloc.h @@ -27,7 +27,8 @@ struct DBArrayAlloc { static void free(void* p, size_type align) noexcept; static void* empty_buf() { - return const_cast (&_empty_buf); + + return (void*)(&_empty_buf + 1); } static constexpr size_type* get_size_ptr(void* p) { return static_cast(p) - 1; @@ -38,7 +39,7 @@ struct DBArrayAlloc { } private: - static const char _empty_buf = '\0'; + static const uint16_t _empty_buf = 0; }; } // namespace lcf