mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-22 10:30:05 +00:00
Fix MSVC warnings
This commit is contained in:
parent
4f7ad14c2b
commit
6cf24c7f92
@ -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'");
|
||||
|
@ -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:
|
||||
|
@ -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);
|
||||
|
11
test/util.h
11
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user