rect: Avoid numeric overflow on massive lines in SDL_IntersectRectAndLine.

Reference Issue #8301.
Reference Issue #8113.

(cherry picked from commit a391dd5fef70e0da4702d355b6331da5bf1f84a2)
main
Ryan C. Gordon 2023-11-24 19:29:39 -05:00
parent e548044a82
commit db7f6425d0
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
2 changed files with 11 additions and 8 deletions

View File

@ -88,6 +88,7 @@ SDL_bool SDL_GetSpanEnclosingRect(int width, int height,
#define RECTTYPE SDL_Rect #define RECTTYPE SDL_Rect
#define POINTTYPE SDL_Point #define POINTTYPE SDL_Point
#define SCALARTYPE int #define SCALARTYPE int
#define BIGSCALARTYPE Sint64
#define COMPUTEOUTCODE ComputeOutCode #define COMPUTEOUTCODE ComputeOutCode
#define SDL_HASINTERSECTION SDL_HasRectIntersection #define SDL_HASINTERSECTION SDL_HasRectIntersection
#define SDL_INTERSECTRECT SDL_GetRectIntersection #define SDL_INTERSECTRECT SDL_GetRectIntersection
@ -100,6 +101,7 @@ SDL_bool SDL_GetSpanEnclosingRect(int width, int height,
#define RECTTYPE SDL_FRect #define RECTTYPE SDL_FRect
#define POINTTYPE SDL_FPoint #define POINTTYPE SDL_FPoint
#define SCALARTYPE float #define SCALARTYPE float
#define BIGSCALARTYPE double
#define COMPUTEOUTCODE ComputeOutCodeFloat #define COMPUTEOUTCODE ComputeOutCodeFloat
#define SDL_HASINTERSECTION SDL_HasRectIntersectionFloat #define SDL_HASINTERSECTION SDL_HasRectIntersectionFloat
#define SDL_INTERSECTRECT SDL_GetRectIntersectionFloat #define SDL_INTERSECTRECT SDL_GetRectIntersectionFloat

View File

@ -376,16 +376,16 @@ SDL_bool SDL_INTERSECTRECTANDLINE(const RECTTYPE *rect, SCALARTYPE *X1, SCALARTY
if (outcode1) { if (outcode1) {
if (outcode1 & CODE_TOP) { if (outcode1 & CODE_TOP) {
y = recty1; y = recty1;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1); x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
} else if (outcode1 & CODE_BOTTOM) { } else if (outcode1 & CODE_BOTTOM) {
y = recty2; y = recty2;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1); x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
} else if (outcode1 & CODE_LEFT) { } else if (outcode1 & CODE_LEFT) {
x = rectx1; x = rectx1;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1); y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
} else if (outcode1 & CODE_RIGHT) { } else if (outcode1 & CODE_RIGHT) {
x = rectx2; x = rectx2;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1); y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
} }
x1 = x; x1 = x;
y1 = y; y1 = y;
@ -394,23 +394,23 @@ SDL_bool SDL_INTERSECTRECTANDLINE(const RECTTYPE *rect, SCALARTYPE *X1, SCALARTY
if (outcode2 & CODE_TOP) { if (outcode2 & CODE_TOP) {
SDL_assert(y2 != y1); /* if equal: division by zero. */ SDL_assert(y2 != y1); /* if equal: division by zero. */
y = recty1; y = recty1;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1); x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
} else if (outcode2 & CODE_BOTTOM) { } else if (outcode2 & CODE_BOTTOM) {
SDL_assert(y2 != y1); /* if equal: division by zero. */ SDL_assert(y2 != y1); /* if equal: division by zero. */
y = recty2; y = recty2;
x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1); x = (SCALARTYPE) (x1 + ((BIGSCALARTYPE)(x2 - x1) * (y - y1)) / (y2 - y1));
} else if (outcode2 & CODE_LEFT) { } else if (outcode2 & CODE_LEFT) {
/* If this assertion ever fires, here's the static analysis that warned about it: /* If this assertion ever fires, here's the static analysis that warned about it:
http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-b0d01a.html#EndPath */ http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-b0d01a.html#EndPath */
SDL_assert(x2 != x1); /* if equal: division by zero. */ SDL_assert(x2 != x1); /* if equal: division by zero. */
x = rectx1; x = rectx1;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1); y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
} else if (outcode2 & CODE_RIGHT) { } else if (outcode2 & CODE_RIGHT) {
/* If this assertion ever fires, here's the static analysis that warned about it: /* If this assertion ever fires, here's the static analysis that warned about it:
http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-39b114.html#EndPath */ http://buildbot.libsdl.org/sdl-static-analysis/sdl-macosx-static-analysis/sdl-macosx-static-analysis-1101/report-39b114.html#EndPath */
SDL_assert(x2 != x1); /* if equal: division by zero. */ SDL_assert(x2 != x1); /* if equal: division by zero. */
x = rectx2; x = rectx2;
y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1); y = (SCALARTYPE) (y1 + ((BIGSCALARTYPE)(y2 - y1) * (x - x1)) / (x2 - x1));
} }
x2 = x; x2 = x;
y2 = y; y2 = y;
@ -427,6 +427,7 @@ SDL_bool SDL_INTERSECTRECTANDLINE(const RECTTYPE *rect, SCALARTYPE *X1, SCALARTY
#undef RECTTYPE #undef RECTTYPE
#undef POINTTYPE #undef POINTTYPE
#undef SCALARTYPE #undef SCALARTYPE
#undef BIGSCALARTYPE
#undef COMPUTEOUTCODE #undef COMPUTEOUTCODE
#undef SDL_HASINTERSECTION #undef SDL_HASINTERSECTION
#undef SDL_INTERSECTRECT #undef SDL_INTERSECTRECT