shithub: cstory

Download patch

ref: 5395b00a953245c3540a6f551937438cf0f85fac
parent: 6456649e11d409bbeb82d93711270123ee41e79d
author: Clownacy <[email protected]>
date: Mon Apr 6 16:05:15 EDT 2020

Make some backend code safer

--- a/src/Backends/GLFW3/Misc.cpp
+++ b/src/Backends/GLFW3/Misc.cpp
@@ -245,7 +245,9 @@
 
 		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);
 	}
--- a/src/Backends/SDL2/Misc.cpp
+++ b/src/Backends/SDL2/Misc.cpp
@@ -2,6 +2,7 @@
 
 #include <stddef.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "SDL.h"
@@ -25,6 +26,7 @@
 
 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 @@
 	if (cursor_surface != NULL)
 		SDL_FreeSurface(cursor_surface);
 
+	free(cursor_surface_pixels);
+
 	SDL_Quit();
 }
 
@@ -78,16 +82,33 @@
 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)