Skip to content

Commit a7ff6e9

Browse files
committed
Fixed overflow in surface pitch calculation
1 parent efe0935 commit a7ff6e9

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/video/SDL_surface.c

+15-8
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,23 @@
2828
#include "SDL_yuv_c.h"
2929

3030

31-
/* Check to make sure we can safely check multiplication of surface w and pitch and it won't overflow size_t */
32-
SDL_COMPILE_TIME_ASSERT(surface_size_assumptions,
33-
sizeof(int) == sizeof(Sint32) && sizeof(size_t) >= sizeof(Sint32));
31+
/* Check to make sure we can safely check multiplication of surface w and pitch and it won't overflow Sint64 */
32+
SDL_COMPILE_TIME_ASSERT(surface_size_assumptions, sizeof(int) == sizeof(Sint32));
3433

3534
/* Public routines */
3635

3736
/*
3837
* Calculate the pad-aligned scanline width of a surface
3938
*/
40-
static int
39+
static Sint64
4140
SDL_CalculatePitch(Uint32 format, int width)
4241
{
43-
int pitch;
42+
Sint64 pitch;
4443

4544
if (SDL_ISPIXELFORMAT_FOURCC(format) || SDL_BITSPERPIXEL(format) >= 8) {
46-
pitch = (width * SDL_BYTESPERPIXEL(format));
45+
pitch = ((Sint64)width * SDL_BYTESPERPIXEL(format));
4746
} else {
48-
pitch = ((width * SDL_BITSPERPIXEL(format)) + 7) / 8;
47+
pitch = (((Sint64)width * SDL_BITSPERPIXEL(format)) + 7) / 8;
4948
}
5049
pitch = (pitch + 3) & ~3; /* 4-byte aligning for speed */
5150
return pitch;
@@ -59,11 +58,19 @@ SDL_Surface *
5958
SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth,
6059
Uint32 format)
6160
{
61+
Sint64 pitch;
6262
SDL_Surface *surface;
6363

6464
/* The flags are no longer used, make the compiler happy */
6565
(void)flags;
6666

67+
pitch = SDL_CalculatePitch(format, width);
68+
if (pitch < 0 || pitch > SDL_MAX_SINT32) {
69+
/* Overflow... */
70+
SDL_OutOfMemory();
71+
return NULL;
72+
}
73+
6774
/* Allocate the surface */
6875
surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
6976
if (surface == NULL) {
@@ -78,7 +85,7 @@ SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth,
7885
}
7986
surface->w = width;
8087
surface->h = height;
81-
surface->pitch = SDL_CalculatePitch(format, width);
88+
surface->pitch = (int)pitch;
8289
SDL_SetClipRect(surface, NULL);
8390

8491
if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {

0 commit comments

Comments
 (0)