mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
Fix MSVC warnings
This commit is contained in:
@ -50,16 +50,6 @@
|
|||||||
#include "mock-allocator.h"
|
#include "mock-allocator.h"
|
||||||
#include "gtest-extra.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 min
|
||||||
#undef max
|
#undef max
|
||||||
|
|
||||||
@ -1456,11 +1446,11 @@ TEST(FormatterTest, FormatExamples) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char *filename = "nonexistent";
|
const char *filename = "nonexistent";
|
||||||
FILE *ftest = fopen(filename, "r");
|
FILE *ftest = safe_fopen(filename, "r");
|
||||||
int error_code = errno;
|
int error_code = errno;
|
||||||
EXPECT_TRUE(ftest == 0);
|
EXPECT_TRUE(ftest == 0);
|
||||||
EXPECT_SYSTEM_ERROR({
|
EXPECT_SYSTEM_ERROR({
|
||||||
FILE *f = fopen(filename, "r");
|
FILE *f = safe_fopen(filename, "r");
|
||||||
if (!f)
|
if (!f)
|
||||||
throw fmt::SystemError(errno, "Cannot open file '{}'", filename);
|
throw fmt::SystemError(errno, "Cannot open file '{}'", filename);
|
||||||
}, error_code, "Cannot open file 'nonexistent'");
|
}, error_code, "Cannot open file 'nonexistent'");
|
||||||
|
@ -40,19 +40,6 @@
|
|||||||
|
|
||||||
namespace {
|
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.
|
// Tests that assertion macros evaluate their arguments exactly once.
|
||||||
class SingleEvaluationTest : public ::testing::Test {
|
class SingleEvaluationTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
|
@ -184,7 +184,7 @@ TEST(FileTest, DefaultCtor) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FileTest, OpenBufferedFileInCtor) {
|
TEST(FileTest, OpenBufferedFileInCtor) {
|
||||||
FILE *fp = fopen("test-file", "w");
|
FILE *fp = safe_fopen("test-file", "w");
|
||||||
std::fputs(FILE_CONTENT, fp);
|
std::fputs(FILE_CONTENT, fp);
|
||||||
std::fclose(fp);
|
std::fclose(fp);
|
||||||
File f("test-file", File::RDONLY);
|
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.
|
// Opens a buffered file for reading.
|
||||||
fmt::BufferedFile open_buffered_file(FILE **fp = 0);
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user