@@ -30,15 +30,42 @@ BOOL readApplicationList(APPLICATION_LIST *applicationList, const wchar_t *path)
3030 return FALSE;
3131 }
3232
33- fread (& applicationList -> count , sizeof (int ), 1 , file );
34- applicationList -> applications = (APPLICATION_SETTINGS * )malloc (sizeof (APPLICATION_SETTINGS ) * applicationList -> count );
35- for (int i = 0 ; i < applicationList -> count ; i ++ )
33+ if (fread (& applicationList -> count , sizeof (int ), 1 , file ) != 1 )
3634 {
37- fread (& applicationList -> applications [i ], sizeof (APPLICATION_SETTINGS ), 1 , file );
35+ applicationList -> count = 0 ;
36+ applicationList -> applications = NULL ;
37+ fclose (file );
38+ return FALSE;
39+ }
40+
41+ if (applicationList -> count > 0 )
42+ {
43+ applicationList -> applications = (APPLICATION_SETTINGS * )malloc (sizeof (APPLICATION_SETTINGS ) * applicationList -> count );
44+ if (applicationList -> applications == NULL )
45+ {
46+ applicationList -> count = 0 ;
47+ fclose (file );
48+ return FALSE;
49+ }
50+
51+ for (int i = 0 ; i < applicationList -> count ; i ++ )
52+ {
53+ if (fread (& applicationList -> applications [i ], sizeof (APPLICATION_SETTINGS ), 1 , file ) != 1 )
54+ {
55+ free (applicationList -> applications );
56+ applicationList -> applications = NULL ;
57+ applicationList -> count = 0 ;
58+ fclose (file );
59+ return FALSE;
60+ }
61+ }
62+ }
63+ else
64+ {
65+ applicationList -> applications = NULL ;
3866 }
3967
4068 fclose (file );
41- _fcloseall ();
4269
4370 return TRUE;
4471}
@@ -64,7 +91,6 @@ BOOL writeApplicationList(APPLICATION_LIST *applicationList, const wchar_t *path
6491 }
6592
6693 fclose (file );
67- _fcloseall ();
6894
6995 return TRUE;
7096}
@@ -76,6 +102,8 @@ BOOL addApplication(APPLICATION_LIST *applicationList, APPLICATION_SETTINGS appl
76102 {
77103 applicationList -> count = 1 ;
78104 applicationList -> applications = (APPLICATION_SETTINGS * )malloc (sizeof (APPLICATION_SETTINGS ));
105+ if (applicationList -> applications == NULL )
106+ return FALSE;
79107 applicationList -> applications [0 ] = application ;
80108 }
81109 else
@@ -87,8 +115,11 @@ BOOL addApplication(APPLICATION_LIST *applicationList, APPLICATION_SETTINGS appl
87115 return FALSE;
88116 }
89117
118+ APPLICATION_SETTINGS * temp = (APPLICATION_SETTINGS * )realloc (applicationList -> applications , sizeof (APPLICATION_SETTINGS ) * (applicationList -> count + 1 ));
119+ if (temp == NULL )
120+ return FALSE;
121+ applicationList -> applications = temp ;
90122 applicationList -> count ++ ;
91- applicationList -> applications = (APPLICATION_SETTINGS * )realloc (applicationList -> applications , sizeof (APPLICATION_SETTINGS ) * applicationList -> count );
92123 applicationList -> applications [applicationList -> count - 1 ] = application ;
93124 }
94125
@@ -119,7 +150,10 @@ BOOL removeApplication(APPLICATION_LIST *applicationList, int index)
119150 }
120151
121152 applicationList -> count -- ;
122- applicationList -> applications = (APPLICATION_SETTINGS * )realloc (applicationList -> applications , sizeof (APPLICATION_SETTINGS ) * applicationList -> count );
153+ APPLICATION_SETTINGS * temp = (APPLICATION_SETTINGS * )realloc (applicationList -> applications , sizeof (APPLICATION_SETTINGS ) * applicationList -> count );
154+ if (temp != NULL )
155+ applicationList -> applications = temp ;
156+ // If realloc fails, keep the old pointer (memory is still valid, just larger than needed)
123157 }
124158
125159 return TRUE;
@@ -153,7 +187,7 @@ BOOL createApplicationDirectory(wchar_t *outPath)
153187 if (!SUCCEEDED (SHGetKnownFolderPath (& FOLDERID_RoamingAppData , 0 , NULL , & path )))
154188 return FALSE;
155189
156- wcscpy (outPath , path );
190+ wcscpy_s (outPath , MAX_PATH , path );
157191
158192 // create directory
159193 PathAppend (outPath , TEXT ("DisplayLock" ));
@@ -173,8 +207,8 @@ BOOL createApplicationSettings(const wchar_t *appPath, APPLICATION_SETTINGS *app
173207 if (basename == appPath )
174208 return FALSE;
175209
176- wcscpy (application -> application_path , appPath );
177- wcscpy (application -> application_name , basename );
210+ wcscpy_s (application -> application_path , MAX_PATH , appPath );
211+ wcscpy_s (application -> application_name , MAX_PATH , basename );
178212 application -> borderless = FALSE;
179213 application -> fullscreen = FALSE;
180214 application -> enabled = TRUE;
@@ -185,7 +219,7 @@ BOOL createApplicationSettings(const wchar_t *appPath, APPLICATION_SETTINGS *app
185219BOOL startApplicationThread (HANDLE * thread , int (* callback )(void * parameters ), void * args )
186220{
187221 // TODO: check better error checking
188- * thread = (HANDLE )_beginthreadex (NULL , 0 , callback , args , 0 , NULL );
222+ * thread = (HANDLE )( uintptr_t ) _beginthreadex (NULL , 0 , callback , args , 0 , NULL );
189223
190224 if (* thread == NULL )
191225 return FALSE;
@@ -194,15 +228,15 @@ BOOL startApplicationThread(HANDLE *thread, int (*callback)(void *parameters), v
194228}
195229
196230// safely closes the thread
197- void closeApplicationThread (HANDLE thread , BOOL * status )
231+ void closeApplicationThread (HANDLE * thread , volatile BOOL * status )
198232{
199233 // check to see if thread is running
200- if (thread != NULL )
234+ if (* thread != NULL )
201235 {
202236 * status = FALSE;
203- WaitForSingleObject (thread , INFINITE );
204- CloseHandle (thread );
205- thread = NULL ;
237+ WaitForSingleObject (* thread , INFINITE );
238+ CloseHandle (* thread );
239+ * thread = NULL ;
206240 }
207241}
208242
@@ -251,62 +285,61 @@ int CALLBACK cursorLockApplications(void *parameters)
251285
252286 while (* (args -> clipRunning ))
253287 {
254- HANDLE mutex = CreateMutex (NULL , FALSE, APPLICATION_MUTEX_NAME );
255- WaitForSingleObject (mutex , INFINITE );
288+ WaitForSingleObject (args -> mutex , INFINITE );
256289
257290 for (int i = 0 ; i < args -> applicationList -> count ; i ++ )
258291 {
259292 APPLICATION_SETTINGS application = args -> applicationList -> applications [i ];
260293
261294 if (application .enabled )
262295 {
263- EnumWindowsProcPIDArgs args ;
264- args .pid = getPidFromName (application .application_name );
265- args .hwnd = NULL ;
296+ EnumWindowsProcPIDArgs enumArgs ;
297+ enumArgs .pid = getPidFromName (application .application_name );
298+ enumArgs .hwnd = NULL ;
266299
267- if (args .pid != 0 )
268- EnumWindows (EnumWindowsProcPID , (LPARAM )& args );
300+ if (enumArgs .pid != 0 )
301+ EnumWindows (EnumWindowsProcPID , (LPARAM )& enumArgs );
269302
270- if (args .hwnd != NULL )
303+ if (enumArgs .hwnd != NULL )
271304 {
272305 RECT rect ;
273- GetWindowRect (args .hwnd , & rect );
306+ GetWindowRect (enumArgs .hwnd , & rect );
274307
275308 if (application .borderless )
276309 {
277- const long long borderlessStyle = GetWindowLongPtr (args .hwnd , GWL_STYLE );
278- const long long borderlessStyleEx = GetWindowLongPtr (args .hwnd , GWL_EXSTYLE );
310+ const long long borderlessStyle = GetWindowLongPtr (enumArgs .hwnd , GWL_STYLE );
311+ const long long borderlessStyleEx = GetWindowLongPtr (enumArgs .hwnd , GWL_EXSTYLE );
279312
280313 const long long mask = WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU | WS_CAPTION ;
281314 const long long exMask = WS_EX_WINDOWEDGE ;
282315
283316 if ((borderlessStyle & mask ) != 0 )
284- SetWindowLongPtr (args .hwnd , GWL_STYLE , borderlessStyle & ~mask );
317+ SetWindowLongPtr (enumArgs .hwnd , GWL_STYLE , borderlessStyle & ~mask );
285318
286319 if ((borderlessStyleEx & exMask ) != 0 )
287- SetWindowLongPtr (args .hwnd , GWL_EXSTYLE , borderlessStyleEx & ~exMask );
320+ SetWindowLongPtr (enumArgs .hwnd , GWL_EXSTYLE , borderlessStyleEx & ~exMask );
288321 }
289322 else if (application .fullscreen )
290323 {
291- RECT rect = {0 };
292- GetClientRect (args .hwnd , & rect );
293- ClientToScreen (args .hwnd , (LPPOINT )& rect .left );
294- ClientToScreen (args .hwnd , (LPPOINT )& rect .right );
324+ RECT fsRect = {0 };
325+ GetClientRect (enumArgs .hwnd , & fsRect );
326+ ClientToScreen (enumArgs .hwnd , (LPPOINT )& fsRect .left );
327+ ClientToScreen (enumArgs .hwnd , (LPPOINT )& fsRect .right );
295328
296- if (rect .left != 0 || rect .top != 0 || rect .right != GetSystemMetrics (SM_CXSCREEN ) || rect .bottom != GetSystemMetrics (SM_CYSCREEN ))
297- SetWindowPos (args .hwnd , NULL , 0 , 0 , GetSystemMetrics (SM_CXSCREEN ), GetSystemMetrics (SM_CYSCREEN ), 0 );
329+ if (fsRect .left != 0 || fsRect .top != 0 || fsRect .right != GetSystemMetrics (SM_CXSCREEN ) || fsRect .bottom != GetSystemMetrics (SM_CYSCREEN ))
330+ SetWindowPos (enumArgs .hwnd , NULL , 0 , 0 , GetSystemMetrics (SM_CXSCREEN ), GetSystemMetrics (SM_CYSCREEN ), 0 );
298331 }
299332
300333 // TODO: lock cursor
301334 HWND active = GetForegroundWindow ();
302335
303- if (args .hwnd == active )
336+ if (enumArgs .hwnd == active )
304337 {
305338 GetCursorPos (& cursorPosition );
306339 RECT windowRect = {0 };
307- GetClientRect (args .hwnd , & windowRect );
308- ClientToScreen (args .hwnd , (LPPOINT )& windowRect .left );
309- ClientToScreen (args .hwnd , (LPPOINT )& windowRect .right );
340+ GetClientRect (enumArgs .hwnd , & windowRect );
341+ ClientToScreen (enumArgs .hwnd , (LPPOINT )& windowRect .left );
342+ ClientToScreen (enumArgs .hwnd , (LPPOINT )& windowRect .right );
310343
311344 if ((cursorPosition .y <= windowRect .bottom && cursorPosition .y >= windowRect .top ) && (cursorPosition .x >= windowRect .left && cursorPosition .x <= windowRect .right ))
312345 ClipCursor (& windowRect );
@@ -315,11 +348,10 @@ int CALLBACK cursorLockApplications(void *parameters)
315348 }
316349 }
317350 }
318- Sleep (1 );
319351 }
320352
321- ReleaseMutex (mutex );
322- CloseHandle ( mutex );
353+ ReleaseMutex (args -> mutex );
354+ Sleep ( 1 );
323355 }
324356
325357 ClipCursor (NULL );
0 commit comments