winpr-comm: added _HandleCreatorsLock to protect accesses to _HandleCreators
authorEmmanuel Ledoux <eledoux@hp.com>
Thu, 3 Jul 2014 09:20:46 +0000 (11:20 +0200)
committerEmmanuel Ledoux <emmanuel.ledoux@hp.com>
Thu, 3 Jul 2014 09:20:46 +0000 (11:20 +0200)
winpr/libwinpr/file/file.c

index 143d5b1..06db37e 100644 (file)
 /* _HandleCreators is a NULL-terminated array with a maximun of HANDLE_CREATOR_MAX HANDLE_CREATOR */
 #define HANDLE_CREATOR_MAX 128
 static HANDLE_CREATOR **_HandleCreators = NULL;
+static CRITICAL_SECTION _HandleCreatorsLock;
 
 static pthread_once_t _HandleCreatorsInitialized = PTHREAD_ONCE_INIT;
 static void _HandleCreatorsInit()
@@ -205,6 +206,8 @@ static void _HandleCreatorsInit()
 
        _HandleCreators = (HANDLE_CREATOR**)calloc(HANDLE_CREATOR_MAX+1, sizeof(HANDLE_CREATOR*));
 
+       InitializeCriticalSection(&_HandleCreatorsLock);
+
        assert(_HandleCreators != NULL);
 }
 
@@ -232,16 +235,23 @@ BOOL RegisterHandleCreator(PHANDLE_CREATOR pHandleCreator)
        }
 
 
+       EnterCriticalSection(&_HandleCreatorsLock);
+
        for (i=0; i<HANDLE_CREATOR_MAX; i++)
        {
                if (_HandleCreators[i] == NULL)
                {
                        _HandleCreators[i] = pHandleCreator;
+
+
+                       LeaveCriticalSection(&_HandleCreatorsLock);
                        return TRUE;
                }
        }
 
        SetLastError(ERROR_INSUFFICIENT_BUFFER);
+
+       LeaveCriticalSection(&_HandleCreatorsLock);
        return FALSE;
 }
 
@@ -302,16 +312,23 @@ HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
                return INVALID_HANDLE_VALUE;
        }
 
+       EnterCriticalSection(&_HandleCreatorsLock);
+
        for (i=0; _HandleCreators[i] != NULL; i++)
        {
                HANDLE_CREATOR *creator = (HANDLE_CREATOR*)_HandleCreators[i];
                if (creator && creator->IsHandled(lpFileName))
                {
-                       return creator->CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
-                                                   dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
+                       HANDLE newHandle = creator->CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
+                                                               dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
+
+                       LeaveCriticalSection(&_HandleCreatorsLock);
+                       return newHandle;
                }
        }
 
+       LeaveCriticalSection(&_HandleCreatorsLock);
+
        /* TODO: use of a HANDLE_CREATOR for named pipes as well */
 
        if (!IsNamedPipeFileNameA(lpFileName))