From 692cbcf8ca3cd90c43d7159da098e0763ecdab38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Podg=C3=B3rski?= Date: Wed, 1 Sep 2021 13:15:39 +0200 Subject: [PATCH] Fix - unsafe _wfopen and _wfreopen in Visual Studio (#206) --- src/miniz.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/miniz.h b/src/miniz.h index a49cf81..8083d3a 100644 --- a/src/miniz.h +++ b/src/miniz.h @@ -4828,9 +4828,16 @@ static wchar_t *str2wstr(const char *str) { } static FILE *mz_fopen(const char *pFilename, const char *pMode) { + FILE *pFile = NULL; + wchar_t *wFilename = str2wstr(pFilename); wchar_t *wMode = str2wstr(pMode); - FILE *pFile = _wfopen(wFilename, wMode); + +#if defined(__MINGW64__) + pFile = _wfopen(wFilename, wMode); +#elif defined(_MSC_VER) + _wfopen_s(&pFile, wFilename, wMode); +#endif free(wFilename); free(wMode); @@ -4839,13 +4846,25 @@ static FILE *mz_fopen(const char *pFilename, const char *pMode) { } static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream) { + FILE *pFile = NULL; + int res = 0; + wchar_t *wPath = str2wstr(pPath); wchar_t *wMode = str2wstr(pMode); - FILE *pFile = _wfreopen(wPath, wMode, pStream); + +#if defined(__MINGW64__) + pFile = _wfreopen(wPath, wMode, pStream); +#elif defined(_MSC_VER) + res = _wfreopen_s(&pFile, wPath, wMode, pStream); +#endif free(wPath); free(wMode); + if (res) { + return NULL; + } + return pFile; }