From 84f6f27788a80fdfa4cece1ee3e8f93586e68923 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Mon, 20 Aug 2018 12:30:55 +0200 Subject: [PATCH] Fix stack-use-after-scope error detected by ASAN in "test_upstream_resource". --- test/monotonic_buffer_resource_test.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/monotonic_buffer_resource_test.cpp b/test/monotonic_buffer_resource_test.cpp index 82e7e54..2f3f0b1 100644 --- a/test/monotonic_buffer_resource_test.cpp +++ b/test/monotonic_buffer_resource_test.cpp @@ -230,6 +230,9 @@ struct derived_from_monotonic_buffer_resource void test_upstream_resource() { + //Allocate buffer first to avoid stack-use-after-scope in monotonic_buffer_resource's destructor + const std::size_t BufSz = monotonic_buffer_resource::initial_next_buffer_size; + boost::move_detail::aligned_storage::type buf; //Test stores the resource and uses it to allocate memory derived_from_memory_resource dmr; dmr.reset(); @@ -240,10 +243,8 @@ void test_upstream_resource() BOOST_TEST(dmbr.current_buffer() == 0); //Test it does not allocate any memory BOOST_TEST(dmr.do_allocate_called == false); - const std::size_t BufSz = monotonic_buffer_resource::initial_next_buffer_size; - //Now allocate storage, and stub it as the return buffer + //Now stub buffer storage it as the return buffer //for "derived_from_memory_resource": - boost::move_detail::aligned_storage::type buf; dmr.do_allocate_return = &buf; //Test that allocation uses the upstream_resource() void *addr = dmbr.do_allocate(1u, 1u);