From e1c86f0b5de96a33d24a1dc139318db5bc5d0d5d Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 6 May 2014 06:58:32 -0700 Subject: [PATCH] Fix warnings. --- test/format-test.cc | 19 +++++++++++++++---- test/gtest-extra-test.cc | 4 +++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/test/format-test.cc b/test/format-test.cc index db5d6a29..e6483246 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -38,6 +38,14 @@ // Check if format.h compiles with windows.h included. #ifdef _WIN32 # include + +// Fix MSVC warning about "unsafe" fopen. +FILE *FOpen(const char *filename, const char *mode) { + FILE *f = 0; + errno = fopen_s(&f, filename, mode); + return f; +} +#define fopen FOpen #endif #include "format.h" @@ -1585,12 +1593,15 @@ TEST(FormatterTest, FormatExamples) { EXPECT_EQ("0123456789", s); } - EXPECT_THROW({ - const char *filename = "nonexistent"; - FILE *f = std::fopen(filename, "r"); + const char *filename = "nonexistent"; + FILE *ftest = fopen(filename, "r"); + int error_code = errno; + EXPECT_TRUE(ftest == 0); + EXPECT_SYSTEM_ERROR({ + FILE *f = fopen(filename, "r"); if (!f) fmt::ThrowSystemError(errno, "Cannot open file '{}'") << filename; - }, fmt::SystemError); + }, error_code, "Cannot open file 'nonexistent'"); } TEST(FormatterTest, StrNamespace) { diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index c354e0b8..790a4049 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -35,6 +35,7 @@ #ifdef _WIN32 # include // for _CrtSetReportMode +# define close _close #endif // _WIN32 namespace { @@ -258,7 +259,8 @@ bool IsClosed(int fd) { // Attempts to read count characters from a file. std::string Read(File &f, std::size_t count) { std::string buffer(count, '\0'); - std::streamsize offset = 0, n = 0; + std::streamsize n = 0; + std::size_t offset = 0; do { n = f.read(&buffer[offset], count - offset); offset += n;