Fixed crash if all displays have been disconnected Fixes https://github.com/libsdl-org/SDL/issues/5867
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
diff --git a/src/video/windows/SDL_windowsevents.c b/src/video/windows/SDL_windowsevents.c
index 4a1cfda..d7f77f7 100644
--- a/src/video/windows/SDL_windowsevents.c
+++ b/src/video/windows/SDL_windowsevents.c
@@ -623,24 +623,24 @@ WIN_KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
static void WIN_CheckICMProfileChanged(SDL_Window* window)
{
- SDL_VideoDisplay* display = SDL_GetDisplayForWindow(window);
- SDL_DisplayData* data = (SDL_DisplayData*)display->driverdata;
- static WCHAR currentIcmFileName[MAX_PATH] = { '\0' };
- WCHAR icmFileName[MAX_PATH];
- HDC hdc;
- SDL_bool succeeded;
- DWORD fileNameSize = MAX_PATH;
-
- hdc = CreateDCW(data->DeviceName, NULL, NULL, NULL);
- if (hdc) {
- succeeded = GetICMProfileW(hdc, &fileNameSize, icmFileName);
- DeleteDC(hdc);
- if (succeeded) {
-
- if (SDL_wcsncmp(currentIcmFileName, icmFileName, fileNameSize)) {
- SDL_wcslcpy(currentIcmFileName, icmFileName, fileNameSize);
- SDL_SendWindowEvent(window, SDL_WINDOWEVENT_ICCPROF_CHANGED, 0, 0);
+ SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
+ SDL_DisplayData *data = display ? (SDL_DisplayData*)display->driverdata : NULL;
+
+ if (data) {
+ HDC hdc = CreateDCW(data->DeviceName, NULL, NULL, NULL);
+ if (hdc) {
+ static WCHAR currentIcmFileName[MAX_PATH];
+ WCHAR icmFileName[MAX_PATH];
+ DWORD fileNameSize = SDL_arraysize(icmFileName);
+ if (GetICMProfileW(hdc, &fileNameSize, icmFileName)) {
+ /* fileNameSize includes '\0' on return */
+ fileNameSize *= sizeof(icmFileName[0]);
+ if (SDL_memcmp(currentIcmFileName, icmFileName, fileNameSize) != 0) {
+ SDL_memcpy(currentIcmFileName, icmFileName, fileNameSize);
+ SDL_SendWindowEvent(window, SDL_WINDOWEVENT_ICCPROF_CHANGED, 0, 0);
+ }
}
+ DeleteDC(hdc);
}
}
}