From ff35f82ba45d294cf8e29dd74e5700f40a5187e6 Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Tue, 17 Sep 2024 19:57:40 +0200 Subject: [PATCH 1/2] Avoid UB in debug code `std::memset` calls with a nullptr argument are flagged as UB by GCC `-fsanitize=undefined`, so check the size first Fixes #39 --- include/boost/circular_buffer/debug.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/boost/circular_buffer/debug.hpp b/include/boost/circular_buffer/debug.hpp index 6559a670..115d65a7 100644 --- a/include/boost/circular_buffer/debug.hpp +++ b/include/boost/circular_buffer/debug.hpp @@ -34,11 +34,16 @@ const int UNINITIALIZED = 0xcc; template inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT { +<<<<<<< HEAD #if defined(__GNUC__) && __GNUC__ < 5 // Avoid warning for calling memset with a constant size of zero if( size_in_bytes ) #endif std::memset(static_cast(data), UNINITIALIZED, size_in_bytes); +======= + if(size_in_bytes) + std::memset(static_cast(data), UNINITIALIZED, size_in_bytes); +>>>>>>> 598d346 (Avoid UB in debug code) } template From e3d691486228c34d7865129c3f194981311a0044 Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Sun, 29 Nov 2026 14:45:33 +0100 Subject: [PATCH 2/2] Assert data pointer is not NULL before access. --- include/boost/circular_buffer/debug.hpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/include/boost/circular_buffer/debug.hpp b/include/boost/circular_buffer/debug.hpp index 115d65a7..f13979e4 100644 --- a/include/boost/circular_buffer/debug.hpp +++ b/include/boost/circular_buffer/debug.hpp @@ -34,16 +34,10 @@ const int UNINITIALIZED = 0xcc; template inline void do_fill_uninitialized_memory(T* data, std::size_t size_in_bytes) BOOST_NOEXCEPT { -<<<<<<< HEAD -#if defined(__GNUC__) && __GNUC__ < 5 - // Avoid warning for calling memset with a constant size of zero - if( size_in_bytes ) -#endif - std::memset(static_cast(data), UNINITIALIZED, size_in_bytes); -======= - if(size_in_bytes) + if(size_in_bytes) { + BOOST_CB_ASSERT(data); std::memset(static_cast(data), UNINITIALIZED, size_in_bytes); ->>>>>>> 598d346 (Avoid UB in debug code) + } } template