From 5d8ba816de69b22fd4ce369bfa4101f37cb66b44 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 2 Feb 2018 19:34:08 -0800 Subject: [PATCH] Fix a segfault in test on glibc 2.26 #551 --- test/util-test.cc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/test/util-test.cc b/test/util-test.cc index 4f4f04c7..051fc0ca 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -743,9 +743,22 @@ TEST(UtilTest, FormatSystemError) { fmt::format_system_error(message, EDOM, "test"); EXPECT_EQ(fmt::format("test: {}", get_system_error(EDOM)), to_string(message)); - message.resize(0); - fmt::format_system_error( - message, EDOM, fmt::string_view(0, std::numeric_limits::max())); + message = fmt::memory_buffer(); + + // Check if std::allocator throws on allocating max size_t / 2 chars. + size_t max_size = std::numeric_limits::max() / 2; + bool throws_on_alloc = false; + try { + std::allocator alloc; + alloc.deallocate(alloc.allocate(max_size), max_size); + } catch (std::bad_alloc) { + throws_on_alloc = true; + } + if (!throws_on_alloc) { + fmt::print("warning: std::allocator allocates {} chars", max_size); + return; + } + fmt::format_system_error(message, EDOM, fmt::string_view(0, max_size)); EXPECT_EQ(fmt::format("error {}", EDOM), to_string(message)); }