Skip to content

Commit

Permalink
Fix - unsafe _wfopen and _wfreopen in Visual Studio (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-- committed Sep 1, 2021
1 parent ffd2d39 commit 692cbcf
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/miniz.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit 692cbcf

Please sign in to comment.