From d163f36d35a1ec61e6e90fca61b9f3094c4d4ec7 Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Thu, 23 Aug 2012 21:57:12 +0900 Subject: [PATCH] Fix Visual C++ specific warnings about security --- taglib/toolkit/tfile.cpp | 20 ++++++++++++ taglib/toolkit/tfilestream.cpp | 56 +++++++++++++++++++++++++++++++--- taglib/toolkit/tstring.cpp | 9 ++++++ 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/taglib/toolkit/tfile.cpp b/taglib/toolkit/tfile.cpp index 09b8fdbc..30cc9fbc 100644 --- a/taglib/toolkit/tfile.cpp +++ b/taglib/toolkit/tfile.cpp @@ -452,12 +452,32 @@ long File::length() bool File::isReadable(const char *file) { + +#if defined(_MSC_VER) && (_MSC_VER >= 1400) // VC++2005 or later + + return _access_s(file, R_OK) == 0; + +#else + return access(file, R_OK) == 0; + +#endif + } bool File::isWritable(const char *file) { + +#if defined(_MSC_VER) && (_MSC_VER >= 1400) // VC++2005 or later + + return _access_s(file, W_OK) == 0; + +#else + return access(file, W_OK) == 0; + +#endif + } //////////////////////////////////////////////////////////////////////////////// diff --git a/taglib/toolkit/tfilestream.cpp b/taglib/toolkit/tfilestream.cpp index e2aebae3..46f05a8d 100644 --- a/taglib/toolkit/tfilestream.cpp +++ b/taglib/toolkit/tfilestream.cpp @@ -87,10 +87,45 @@ FileStream::FileStreamPrivate::FileStreamPrivate(FileName fileName, bool openRea { // First try with read / write mode, if that fails, fall back to read only. -#ifdef _WIN32 +#if defined(_MSC_VER) && (_MSC_VER >= 1400) // VC++2005 or later + errno_t err; if(wcslen((const wchar_t *) fileName) > 0) { + if(openReadOnly) + err = _wfopen_s(&file, name, L"rb"); + else { + err = _wfopen_s(&file, name, L"rb+"); + + if(err == 0) + readOnly = false; + else + err = _wfopen_s(&file, name, L"rb"); + } + + if(err == 0) + return; + } + + if(openReadOnly) + err = fopen_s(&file, name, "rb"); + else { + err = fopen_s(&file, name, "rb+"); + + if(err == 0) + readOnly = false; + else + err = fopen_s(&file, name, "rb"); + } + + if(err != 0) { + debug("Could not open file " + String((const char *) name)); + } + +#else // defined(_MSC_VER) && (_MSC_VER >= 1400) +# ifdef _WIN32 + + if(wcslen((const wchar_t *) fileName) > 0) { if(openReadOnly) file = _wfopen(name, L"rb"); else { @@ -104,10 +139,9 @@ FileStream::FileStreamPrivate::FileStreamPrivate(FileName fileName, bool openRea if(file) return; - } -#endif +# endif // defined(_WIN32) if(openReadOnly) file = fopen(name, "rb"); @@ -120,10 +154,12 @@ FileStream::FileStreamPrivate::FileStreamPrivate(FileName fileName, bool openRea file = fopen(name, "rb"); } - if(!file) - { + if(!file) { debug("Could not open file " + String((const char *) name)); } + +#endif // defined(_MSC_VER) && (_MSC_VER >= 1400) + } //////////////////////////////////////////////////////////////////////////////// @@ -379,7 +415,17 @@ long FileStream::length() void FileStream::truncate(long length) { + +#if defined(_MSC_VER) && (_MSC_VER >= 1400) // VC++2005 or later + + ftruncate(_fileno(d->file), length); + +#else + ftruncate(fileno(d->file), length); + +#endif + } TagLib::uint FileStream::bufferSize() diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index f99d1c37..bad39749 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -259,8 +259,17 @@ const char *String::toCString(bool unicode) const std::string buffer = to8Bit(unicode); d->CString = new char[buffer.size() + 1]; + +#if defined(_MSC_VER) && (_MSC_VER >= 1400) // VC++2005 or later + + strcpy_s(d->CString, buffer.size() + 1, buffer.c_str()); + +#else + strcpy(d->CString, buffer.c_str()); +#endif + return d->CString; } -- 2.40.0