From 04c6e582be42dbce428d5f04ca14a8d23caafedd Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Tue, 23 Nov 2021 02:01:37 +0300 Subject: [PATCH] Use volatile to make sure globals_retainer is not optimized away. By using volatile qualifier for the internal member of the globals_retainer class we ensure the constructor formally has observable behavior, which means it cannot be optimized away. Related to https://github.com/boostorg/filesystem/issues/217. --- src/path.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/path.cpp b/src/path.cpp index 4cb1804..7b04e6c 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -1568,9 +1568,9 @@ extern const init_func_ptr_t p_init_path_globals = &init_path_globals; //! Makes sure the global initializer pointers are referenced and not removed by linker struct globals_retainer { - const init_func_ptr_t* const m_p_init_path_globals; + const init_func_ptr_t* volatile m_p_init_path_globals; - globals_retainer() : m_p_init_path_globals(&p_init_path_globals) {} + globals_retainer() { m_p_init_path_globals = &p_init_path_globals; } }; BOOST_ATTRIBUTE_UNUSED static const globals_retainer g_globals_retainer