Commit d9e1036e0f88601f600c1d1226793dab5c37a8c7

Sam Lantinga 2017-10-06T16:17:50

Fixed potential overflow in surface allocation (thanks Yves!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c
index cabe7e0..721c477 100644
--- a/src/video/SDL_surface.c
+++ b/src/video/SDL_surface.c
@@ -80,7 +80,15 @@ SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth,
 
     /* Get the pixels */
     if (surface->w && surface->h) {
-        surface->pixels = SDL_malloc(surface->h * surface->pitch);
+        int size = (surface->h * surface->pitch);
+        if (size < 0 || (size / surface->pitch) != surface->h) {
+            /* Overflow... */
+            SDL_FreeSurface(surface);
+            SDL_OutOfMemory();
+            return NULL;
+        }
+
+        surface->pixels = SDL_malloc(size);
         if (!surface->pixels) {
             SDL_FreeSurface(surface);
             SDL_OutOfMemory();