From 5ee969e4f6b9bf5bbb9de6fe418095f4d25d73a7 Mon Sep 17 00:00:00 2001 From: Andrey Bugaevskiy Date: Fri, 11 Mar 2022 19:22:45 +0000 Subject: [PATCH] Fix fopen_s error reporting with PREVENT_CHILD_FD --- include/spdlog/details/os-inl.h | 2 +- tests/test_file_helper.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h index e094e0f7..c3bf691c 100644 --- a/include/spdlog/details/os-inl.h +++ b/include/spdlog/details/os-inl.h @@ -145,7 +145,7 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename const int fd = ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644)); if (fd == -1) { - return false; + return true; } *fp = ::fdopen(fd, mode.c_str()); if (*fp == nullptr) diff --git a/tests/test_file_helper.cpp b/tests/test_file_helper.cpp index 9d459277..1d947078 100644 --- a/tests/test_file_helper.cpp +++ b/tests/test_file_helper.cpp @@ -154,3 +154,15 @@ TEST_CASE("file_event_handlers", "[file_helper]") REQUIRE(events == std::vector{flags::before_close, flags::after_close}); REQUIRE(file_contents(TEST_FILENAME) == "after_open\nbefore_close\n"); } + +TEST_CASE("file_helper_open", "[file_helper]") +{ + prepare_logdir(); + spdlog::filename_t target_filename = SPDLOG_FILENAME_T(TEST_FILENAME); + file_helper helper; + helper.open(target_filename); + helper.close(); + + target_filename += SPDLOG_FILENAME_T("/invalid"); + REQUIRE_THROWS_AS(helper.open(target_filename), spdlog::spdlog_ex); +}