Browse Source

Fixed crash scaling very large surfaces

Fixes https://github.com/libsdl-org/SDL/issues/8878

(cherry picked from commit 09ba55b462b826070482a586b72906fae4a66b32)
Sam Lantinga 1 year ago
parent
commit
f7ab765b84
4 changed files with 307 additions and 306 deletions
  1. 280 280
      src/video/SDL_blit_auto.c
  2. 5 5
      src/video/SDL_blit_slow.c
  3. 17 16
      src/video/SDL_stretch.c
  4. 5 5
      src/video/sdlgenblit.pl

File diff suppressed because it is too large
+ 280 - 280
src/video/SDL_blit_auto.c


+ 5 - 5
src/video/SDL_blit_slow.c

@@ -54,9 +54,9 @@ void SDL_Blit_Slow(SDL_BlitInfo *info)
     Uint32 srcR, srcG, srcB, srcA;
     Uint32 srcR, srcG, srcB, srcA;
     Uint32 dstpixel;
     Uint32 dstpixel;
     Uint32 dstR, dstG, dstB, dstA;
     Uint32 dstR, dstG, dstB, dstA;
-    int srcy, srcx;
-    Uint32 posy, posx;
-    int incy, incx;
+    Uint64 srcy, srcx;
+    Uint64 posy, posx;
+    Uint64 incy, incx;
     SDL_PixelFormat *src_fmt = info->src_fmt;
     SDL_PixelFormat *src_fmt = info->src_fmt;
     SDL_PixelFormat *dst_fmt = info->dst_fmt;
     SDL_PixelFormat *dst_fmt = info->dst_fmt;
     int srcbpp = src_fmt->BytesPerPixel;
     int srcbpp = src_fmt->BytesPerPixel;
@@ -69,8 +69,8 @@ void SDL_Blit_Slow(SDL_BlitInfo *info)
     srcfmt_val = detect_format(src_fmt);
     srcfmt_val = detect_format(src_fmt);
     dstfmt_val = detect_format(dst_fmt);
     dstfmt_val = detect_format(dst_fmt);
 
 
-    incy = (info->src_h << 16) / info->dst_h;
-    incx = (info->src_w << 16) / info->dst_w;
+    incy = ((Uint64)info->src_h << 16) / info->dst_h;
+    incx = ((Uint64)info->src_w << 16) / info->dst_w;
     posy = incy / 2; /* start at the middle of pixel */
     posy = incy / 2; /* start at the middle of pixel */
 
 
     while (info->dst_h--) {
     while (info->dst_h--) {

+ 17 - 16
src/video/SDL_stretch.c

@@ -163,7 +163,8 @@ static int SDL_UpperSoftStretch(SDL_Surface *src, const SDL_Rect *srcrect,
 #define BILINEAR___HEIGHT                                              \
 #define BILINEAR___HEIGHT                                              \
     int index_h, frac_h0, frac_h1, middle;                             \
     int index_h, frac_h0, frac_h1, middle;                             \
     const Uint32 *src_h0, *src_h1;                                     \
     const Uint32 *src_h0, *src_h1;                                     \
-    int no_padding, incr_h0, incr_h1;                                  \
+    int no_padding;                                                    \
+    Uint64 incr_h0, incr_h1;                                           \
                                                                        \
                                                                        \
     no_padding = !(i < left_pad_h || i > dst_h - 1 - right_pad_h);     \
     no_padding = !(i < left_pad_h || i > dst_h - 1 - right_pad_h);     \
     index_h = SRC_INDEX(fp_sum_h);                                     \
     index_h = SRC_INDEX(fp_sum_h);                                     \
@@ -172,7 +173,7 @@ static int SDL_UpperSoftStretch(SDL_Surface *src, const SDL_Rect *srcrect,
     index_h = no_padding ? index_h : (i < left_pad_h ? 0 : src_h - 1); \
     index_h = no_padding ? index_h : (i < left_pad_h ? 0 : src_h - 1); \
     frac_h0 = no_padding ? frac_h0 : 0;                                \
     frac_h0 = no_padding ? frac_h0 : 0;                                \
     incr_h1 = no_padding ? src_pitch : 0;                              \
     incr_h1 = no_padding ? src_pitch : 0;                              \
-    incr_h0 = index_h * src_pitch;                                     \
+    incr_h0 = (Uint64)index_h * src_pitch;                             \
                                                                        \
                                                                        \
     src_h0 = (const Uint32 *)((const Uint8 *)src + incr_h0);           \
     src_h0 = (const Uint32 *)((const Uint8 *)src + incr_h0);           \
     src_h1 = (const Uint32 *)((const Uint8 *)src_h0 + incr_h1);        \
     src_h1 = (const Uint32 *)((const Uint8 *)src_h0 + incr_h1);        \
@@ -824,16 +825,16 @@ int SDL_LowerSoftStretchLinear(SDL_Surface *s, const SDL_Rect *srcrect,
     return ret;
     return ret;
 }
 }
 
 
-#define SDL_SCALE_NEAREST__START       \
-    int i;                             \
-    Uint32 posy, incy;                 \
-    Uint32 posx, incx;                 \
-    int dst_gap;                       \
-    int srcy, n;                       \
-    const Uint32 *src_h0;              \
-    incy = (src_h << 16) / dst_h;      \
-    incx = (src_w << 16) / dst_w;      \
-    dst_gap = dst_pitch - bpp * dst_w; \
+#define SDL_SCALE_NEAREST__START          \
+    int i;                                \
+    Uint64 posy, incy;                    \
+    Uint64 posx, incx;                    \
+    Uint64 srcy, srcx;                    \
+    int dst_gap, n;                       \
+    const Uint32 *src_h0;                 \
+    incy = ((Uint64)src_h << 16) / dst_h; \
+    incx = ((Uint64)src_w << 16) / dst_w; \
+    dst_gap = dst_pitch - bpp * dst_w;    \
     posy = incy / 2;
     posy = incy / 2;
 
 
 #define SDL_SCALE_NEAREST__HEIGHT                                         \
 #define SDL_SCALE_NEAREST__HEIGHT                                         \
@@ -852,7 +853,7 @@ static int scale_mat_nearest_1(const Uint32 *src_ptr, int src_w, int src_h, int
         SDL_SCALE_NEAREST__HEIGHT
         SDL_SCALE_NEAREST__HEIGHT
         while (n--) {
         while (n--) {
             const Uint8 *src;
             const Uint8 *src;
-            int srcx = bpp * (posx >> 16);
+            srcx = bpp * (posx >> 16);
             posx += incx;
             posx += incx;
             src = (const Uint8 *)src_h0 + srcx;
             src = (const Uint8 *)src_h0 + srcx;
             *(Uint8 *)dst = *src;
             *(Uint8 *)dst = *src;
@@ -872,7 +873,7 @@ static int scale_mat_nearest_2(const Uint32 *src_ptr, int src_w, int src_h, int
         SDL_SCALE_NEAREST__HEIGHT
         SDL_SCALE_NEAREST__HEIGHT
         while (n--) {
         while (n--) {
             const Uint16 *src;
             const Uint16 *src;
-            int srcx = bpp * (posx >> 16);
+            srcx = bpp * (posx >> 16);
             posx += incx;
             posx += incx;
             src = (const Uint16 *)((const Uint8 *)src_h0 + srcx);
             src = (const Uint16 *)((const Uint8 *)src_h0 + srcx);
             *(Uint16 *)dst = *src;
             *(Uint16 *)dst = *src;
@@ -892,7 +893,7 @@ static int scale_mat_nearest_3(const Uint32 *src_ptr, int src_w, int src_h, int
         SDL_SCALE_NEAREST__HEIGHT
         SDL_SCALE_NEAREST__HEIGHT
         while (n--) {
         while (n--) {
             const Uint8 *src;
             const Uint8 *src;
-            int srcx = bpp * (posx >> 16);
+            srcx = bpp * (posx >> 16);
             posx += incx;
             posx += incx;
             src = (const Uint8 *)src_h0 + srcx;
             src = (const Uint8 *)src_h0 + srcx;
             ((Uint8 *)dst)[0] = src[0];
             ((Uint8 *)dst)[0] = src[0];
@@ -914,7 +915,7 @@ static int scale_mat_nearest_4(const Uint32 *src_ptr, int src_w, int src_h, int
         SDL_SCALE_NEAREST__HEIGHT
         SDL_SCALE_NEAREST__HEIGHT
         while (n--) {
         while (n--) {
             const Uint32 *src;
             const Uint32 *src;
-            int srcx = bpp * (posx >> 16);
+            srcx = bpp * (posx >> 16);
             posx += incx;
             posx += incx;
             src = (const Uint32 *)((const Uint8 *)src_h0 + srcx);
             src = (const Uint32 *)((const Uint8 *)src_h0 + srcx);
             *dst = *src;
             *dst = *src;

+ 5 - 5
src/video/sdlgenblit.pl

@@ -518,15 +518,15 @@ __EOF__
     }
     }
     if ( $scale ) {
     if ( $scale ) {
         print FILE <<__EOF__;
         print FILE <<__EOF__;
-    int srcy, srcx;
-    Uint32 posy, posx;
-    int incy, incx;
+    Uint64 srcy, srcx;
+    Uint64 posy, posx;
+    Uint64 incy, incx;
 __EOF__
 __EOF__
 
 
     print FILE <<__EOF__;
     print FILE <<__EOF__;
 
 
-    incy = (info->src_h << 16) / info->dst_h;
-    incx = (info->src_w << 16) / info->dst_w;
+    incy = ((Uint64)info->src_h << 16) / info->dst_h;
+    incx = ((Uint64)info->src_w << 16) / info->dst_w;
     posy = incy / 2;
     posy = incy / 2;
 
 
     while (info->dst_h--) {
     while (info->dst_h--) {

Some files were not shown because too many files changed in this diff