From 3f197c3cab3de9a325100d618413e6e6fa0f5da9 Mon Sep 17 00:00:00 2001 From: Moritz Klammler Date: Wed, 28 Sep 2022 19:51:32 +0200 Subject: [PATCH] Fix use-after-move in test_parse_function_compiles and refactor My recent patch had introduced a conditional use-after-move bug into the test_parse_function_compiles function. This patch fixes that by reworking the entire test case into a compile-time check. In my opinion, we're not loosing anything by not actually executing the code (the result wasn't looked at anyway) and the code becomes much clearer by omitting the argument-preparation fluff. --- tests/test_parse_file.cpp | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/tests/test_parse_file.cpp b/tests/test_parse_file.cpp index cea5387..2c45670 100644 --- a/tests/test_parse_file.cpp +++ b/tests/test_parse_file.cpp @@ -989,38 +989,18 @@ BOOST_AUTO_TEST_CASE(test_file_ends_without_lf) BOOST_AUTO_TEST_CASE(test_parse_function_compiles) { - const auto c = [](std::string& s) -> const std::string& { return s; }; - /*mutable*/ std::string example = testinput("example.toml"); - - // toml::parse(""); using result_type = decltype(toml::parse("string literal")); - - BOOST_TEST_MESSAGE("string_literal"); - - // toml::parse(const char*); - const result_type cstring = toml::parse(example.c_str()); - - BOOST_TEST_MESSAGE("const char*"); - - // toml::parse(char*); - const result_type char_ptr = toml::parse(&example.front()); - - BOOST_TEST_MESSAGE("char*"); - - // toml::parse(const std::string&); - const result_type string = toml::parse(c(example)); - // toml::parse(std::string&); - const result_type string_mutref = toml::parse(example); - // toml::parse(std::string&&); - const result_type string_rref = toml::parse(std::move(example)); - - BOOST_TEST_MESSAGE("strings"); - + (void) [](const char* that) -> result_type { return toml::parse(that); }; + (void) [](char* that) -> result_type { return toml::parse(that); }; + (void) [](const std::string& that) -> result_type { return toml::parse(that); }; + (void) [](std::string& that) -> result_type { return toml::parse(that); }; + (void) [](std::string&& that) -> result_type { return toml::parse(that); }; #ifdef TOML11_HAS_STD_FILESYSTEM - const std::filesystem::path fname_path(example.begin(), example.end()); - const result_type filesystem_path = toml::parse(fname_path); - BOOST_TEST_MESSAGE("path"); + (void) [](const std::filesystem::path& that) -> result_type { return toml::parse(that); }; + (void) [](std::filesystem::path& that) -> result_type { return toml::parse(that); }; + (void) [](std::filesystem::path&& that) -> result_type { return toml::parse(that); }; #endif + (void) [](std::FILE* that) -> result_type { return toml::parse(that, "mandatory.toml"); }; } BOOST_AUTO_TEST_CASE(test_parse_nonexistent_file)