From 5395b00a953245c3540a6f551937438cf0f85fac Mon Sep 17 00:00:00 2001 From: Clownacy Date: Mon, 6 Apr 2020 20:05:15 +0100 Subject: [PATCH] Make some backend code safer --- src/Backends/GLFW3/Misc.cpp | 4 +++- src/Backends/SDL2/Misc.cpp | 33 +++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Backends/GLFW3/Misc.cpp b/src/Backends/GLFW3/Misc.cpp index 50945b37..73240b38 100644 --- a/src/Backends/GLFW3/Misc.cpp +++ b/src/Backends/GLFW3/Misc.cpp @@ -245,7 +245,9 @@ void Backend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsi GLFWimage glfw_image = {(int)width, (int)height, rgba_pixels}; cursor = glfwCreateCursor(&glfw_image, 0, 0); - glfwSetCursor(window, cursor); + + if (cursor != NULL) + glfwSetCursor(window, cursor); free(rgba_pixels); } diff --git a/src/Backends/SDL2/Misc.cpp b/src/Backends/SDL2/Misc.cpp index 0beec4b9..d6da3196 100644 --- a/src/Backends/SDL2/Misc.cpp +++ b/src/Backends/SDL2/Misc.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "SDL.h" @@ -25,6 +26,7 @@ BOOL bActive = TRUE; static BOOL keyboard_state[BACKEND_KEYBOARD_TOTAL]; +static unsigned char *cursor_surface_pixels; static SDL_Surface *cursor_surface; static SDL_Cursor *cursor; @@ -50,6 +52,8 @@ void Backend_Deinit(void) if (cursor_surface != NULL) SDL_FreeSurface(cursor_surface); + free(cursor_surface_pixels); + SDL_Quit(); } @@ -78,16 +82,33 @@ void Backend_HideMouse(void) void Backend_SetWindowIcon(const unsigned char *rgb_pixels, unsigned int width, unsigned int height) { SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom((void*)rgb_pixels, width, height, 0, width * 3, SDL_PIXELFORMAT_RGB24); - SDL_SetWindowIcon(window, surface); - SDL_FreeSurface(surface); + + if (surface != NULL) + { + SDL_SetWindowIcon(window, surface); + SDL_FreeSurface(surface); + } } void Backend_SetCursor(const unsigned char *rgb_pixels, unsigned int width, unsigned int height) { - cursor_surface = SDL_CreateRGBSurfaceWithFormatFrom((void*)rgb_pixels, width, height, 0, width * 3, SDL_PIXELFORMAT_RGB24); - SDL_SetColorKey(cursor_surface, SDL_TRUE, SDL_MapRGB(cursor_surface->format, 0xFF, 0, 0xFF)); - cursor = SDL_CreateColorCursor(cursor_surface, 0, 0); - SDL_SetCursor(cursor); + cursor_surface_pixels = (unsigned char*)malloc(width * height * 3); + + if (cursor_surface_pixels != NULL) + { + cursor_surface = SDL_CreateRGBSurfaceWithFormatFrom(cursor_surface_pixels, width, height, 0, width * 3, SDL_PIXELFORMAT_RGB24); + + if (cursor_surface != NULL) + { + if (SDL_SetColorKey(cursor_surface, SDL_TRUE, SDL_MapRGB(cursor_surface->format, 0xFF, 0, 0xFF)) == 0) + { + cursor = SDL_CreateColorCursor(cursor_surface, 0, 0); + + if (cursor != NULL) + SDL_SetCursor(cursor); + } + } + } } void PlaybackBackend_EnableDragAndDrop(void)