28
28
#include "SDL_yuv_c.h"
29
29
30
30
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 ));
34
33
35
34
/* Public routines */
36
35
37
36
/*
38
37
* Calculate the pad-aligned scanline width of a surface
39
38
*/
40
- static int
39
+ static Sint64
41
40
SDL_CalculatePitch (Uint32 format , int width )
42
41
{
43
- int pitch ;
42
+ Sint64 pitch ;
44
43
45
44
if (SDL_ISPIXELFORMAT_FOURCC (format ) || SDL_BITSPERPIXEL (format ) >= 8 ) {
46
- pitch = (width * SDL_BYTESPERPIXEL (format ));
45
+ pitch = (( Sint64 ) width * SDL_BYTESPERPIXEL (format ));
47
46
} else {
48
- pitch = ((width * SDL_BITSPERPIXEL (format )) + 7 ) / 8 ;
47
+ pitch = ((( Sint64 ) width * SDL_BITSPERPIXEL (format )) + 7 ) / 8 ;
49
48
}
50
49
pitch = (pitch + 3 ) & ~3 ; /* 4-byte aligning for speed */
51
50
return pitch ;
@@ -59,11 +58,19 @@ SDL_Surface *
59
58
SDL_CreateRGBSurfaceWithFormat (Uint32 flags , int width , int height , int depth ,
60
59
Uint32 format )
61
60
{
61
+ Sint64 pitch ;
62
62
SDL_Surface * surface ;
63
63
64
64
/* The flags are no longer used, make the compiler happy */
65
65
(void )flags ;
66
66
67
+ pitch = SDL_CalculatePitch (format , width );
68
+ if (pitch < 0 || pitch > SDL_MAX_SINT32 ) {
69
+ /* Overflow... */
70
+ SDL_OutOfMemory ();
71
+ return NULL ;
72
+ }
73
+
67
74
/* Allocate the surface */
68
75
surface = (SDL_Surface * ) SDL_calloc (1 , sizeof (* surface ));
69
76
if (surface == NULL ) {
@@ -78,7 +85,7 @@ SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth,
78
85
}
79
86
surface -> w = width ;
80
87
surface -> h = height ;
81
- surface -> pitch = SDL_CalculatePitch ( format , width ) ;
88
+ surface -> pitch = ( int ) pitch ;
82
89
SDL_SetClipRect (surface , NULL );
83
90
84
91
if (SDL_ISPIXELFORMAT_INDEXED (surface -> format -> format )) {
0 commit comments