From 60109329847c0455a64f19443bff92f281092889 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Mon, 14 Dec 2015 09:23:42 +0100 Subject: [PATCH] Fixed possible race on file creation. --- winpr/libwinpr/file/file.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/winpr/libwinpr/file/file.c b/winpr/libwinpr/file/file.c index 6cd848280..b0eb565b8 100644 --- a/winpr/libwinpr/file/file.c +++ b/winpr/libwinpr/file/file.c @@ -456,6 +456,7 @@ static HANDLE FileCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dw BOOL create; const char* mode = FileGetMode(dwDesiredAccess, dwCreationDisposition, &create); int lock; + FILE* fp = NULL; pFile = (WINPR_FILE*) calloc(1, sizeof(WINPR_FILE)); if (!pFile) @@ -484,22 +485,24 @@ static HANDLE FileCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dw if (create) { - FILE* fp = fopen(pFile->lpFileName, "ab"); + fp = fopen(pFile->lpFileName, "ab"); if (!fp) { free(pFile->lpFileName); free(pFile); return INVALID_HANDLE_VALUE; } - fclose(fp); + + fp = freopen(pFile->lpFileName, mode, fp); } - { - FILE* fp = fopen(pFile->lpFileName, mode); - pFile->fd = -1; - if (fp) - pFile->fd = fileno(fp); - } + if (NULL == fp) + fp = fopen(pFile->lpFileName, mode); + + pFile->fd = -1; + if (fp) + pFile->fd = fileno(fp); + if (pFile->fd < 0) { WLog_ERR(TAG, "Failed to open file %s with mode %s",