From 6cf24c7f920eff03688ff9544644117e249f4e85 Mon Sep 17 00:00:00 2001 From: vitaut Date: Wed, 8 Jul 2015 08:04:32 -0700 Subject: [PATCH] Fix MSVC warnings --- test/format-test.cc | 14 ++------------ test/gtest-extra-test.cc | 13 ------------- test/posix-test.cc | 2 +- test/util.h | 11 +++++++++++ 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/test/format-test.cc b/test/format-test.cc index 31593833..4f6597a4 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -50,16 +50,6 @@ #include "mock-allocator.h" #include "gtest-extra.h" -#if defined(_WIN32) && !defined(__MINGW32__) -// Fix MSVC warning about "unsafe" fopen. -FILE *safe_fopen(const char *filename, const char *mode) { - FILE *f = 0; - errno = fopen_s(&f, filename, mode); - return f; -} -#define fopen safe_fopen -#endif - #undef min #undef max @@ -1456,11 +1446,11 @@ TEST(FormatterTest, FormatExamples) { } const char *filename = "nonexistent"; - FILE *ftest = fopen(filename, "r"); + FILE *ftest = safe_fopen(filename, "r"); int error_code = errno; EXPECT_TRUE(ftest == 0); EXPECT_SYSTEM_ERROR({ - FILE *f = fopen(filename, "r"); + FILE *f = safe_fopen(filename, "r"); if (!f) throw fmt::SystemError(errno, "Cannot open file '{}'", filename); }, error_code, "Cannot open file 'nonexistent'"); diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 12c893c8..1d383cbc 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -40,19 +40,6 @@ namespace { -#ifdef _MSC_VER -// Fix "secure" warning about using fopen without defining -// _CRT_SECURE_NO_WARNINGS. -FILE *safe_fopen(const char *filename, const char *mode) { - FILE *f = 0; - errno = fopen_s(&f, filename, mode); - return f; -} -#define fopen safe_fopen -#else -using std::fopen; -#endif // _MSC_VER - // Tests that assertion macros evaluate their arguments exactly once. class SingleEvaluationTest : public ::testing::Test { protected: diff --git a/test/posix-test.cc b/test/posix-test.cc index 725c1507..4a9db7e3 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -184,7 +184,7 @@ TEST(FileTest, DefaultCtor) { } TEST(FileTest, OpenBufferedFileInCtor) { - FILE *fp = fopen("test-file", "w"); + FILE *fp = safe_fopen("test-file", "w"); std::fputs(FILE_CONTENT, fp); std::fclose(fp); File f("test-file", File::RDONLY); diff --git a/test/util.h b/test/util.h index 4027685c..a0d485eb 100644 --- a/test/util.h +++ b/test/util.h @@ -56,3 +56,14 @@ extern const char *const FILE_CONTENT; // Opens a buffered file for reading. fmt::BufferedFile open_buffered_file(FILE **fp = 0); + +inline FILE *safe_fopen(const char *filename, const char *mode) { +#if defined(_WIN32) && !defined(__MINGW32__) + // Fix MSVC warning about "unsafe" fopen. + FILE *f = 0; + errno = fopen_s(&f, filename, mode); + return f; +#else + return std::fopen(filename, mode); +#endif +}