Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fix bug 1789: SDL_IntersectRect intersection with empty rect does not…
Browse files Browse the repository at this point in the history
… set result to empty; add test coverage to Rect suite
  • Loading branch information
ferzkopp committed Apr 17, 2013
1 parent ea5011f commit 5239c67
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 16 deletions.
86 changes: 72 additions & 14 deletions src/video/SDL_rect.c
Expand Up @@ -29,8 +29,13 @@ SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
{
int Amin, Amax, Bmin, Bmax;

if (!A || !B) {
// TODO error message
if (!A) {
SDL_InvalidParamError("A");
return SDL_FALSE;
}

if (!B) {
SDL_InvalidParamError("B");
return SDL_FALSE;
}

Expand Down Expand Up @@ -71,13 +76,25 @@ SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
{
int Amin, Amax, Bmin, Bmax;

if (!A || !B || !result) {
// TODO error message
if (!A) {
SDL_InvalidParamError("A");
return SDL_FALSE;
}

if (!B) {
SDL_InvalidParamError("B");
return SDL_FALSE;
}

if (!result) {
SDL_InvalidParamError("result");
return SDL_FALSE;
}

/* Special cases for empty rects */
if (SDL_RectEmpty(A) || SDL_RectEmpty(B)) {
result->w = 0;
result->h = 0;
return SDL_FALSE;
}

Expand Down Expand Up @@ -113,7 +130,18 @@ SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
{
int Amin, Amax, Bmin, Bmax;

if (!A || !B || !result) {
if (!A) {
SDL_InvalidParamError("A");
return;
}

if (!B) {
SDL_InvalidParamError("B");
return;
}

if (!result) {
SDL_InvalidParamError("result");
return;
}

Expand Down Expand Up @@ -171,12 +199,12 @@ SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
int x, y, i;

if (!points) {
/* TODO error message */
SDL_InvalidParamError("points");
return SDL_FALSE;
}

if (count < 1) {
/* TODO error message */
SDL_InvalidParamError("count");
return SDL_FALSE;
}

Expand Down Expand Up @@ -298,8 +326,28 @@ SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
int recty2;
int outcode1, outcode2;

if (!rect || !X1 || !Y1 || !X2 || !Y2) {
// TODO error message
if (!rect) {
SDL_InvalidParamError("rect");
return SDL_FALSE;
}

if (!X1) {
SDL_InvalidParamError("X1");
return SDL_FALSE;
}

if (!Y1) {
SDL_InvalidParamError("Y1");
return SDL_FALSE;
}

if (!X2) {
SDL_InvalidParamError("X2");
return SDL_FALSE;
}

if (!Y2) {
SDL_InvalidParamError("Y2");
return SDL_FALSE;
}

Expand Down Expand Up @@ -418,18 +466,28 @@ SDL_GetSpanEnclosingRect(int width, int height,
int span_y1, span_y2;
int rect_y1, rect_y2;

if (width < 1 || height < 1) {
// TODO error message
if (width < 1) {
SDL_InvalidParamError("width");
return SDL_FALSE;
}

if (height < 1) {
SDL_InvalidParamError("height");
return SDL_FALSE;
}

if (!rects) {
SDL_InvalidParamError("rects");
return SDL_FALSE;
}

if (!rects || !span) {
// TODO error message
if (!span) {
SDL_InvalidParamError("span");
return SDL_FALSE;
}

if (numrects < 1) {
// TODO error message
SDL_InvalidParamError("numrects");
return SDL_FALSE;
}

Expand Down
17 changes: 15 additions & 2 deletions test/testautomation_rect.c
Expand Up @@ -614,8 +614,11 @@ int rect_testIntersectRectEmpty (void *arg)
SDL_Rect rectB;
SDL_Rect result;
SDL_bool intersection;
SDL_bool empty;

// Rect A empty
result.w = SDLTest_RandomIntegerInRange(1, 100);
result.h = SDLTest_RandomIntegerInRange(1, 100);
refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
Expand All @@ -627,8 +630,12 @@ int rect_testIntersectRectEmpty (void *arg)
rectB = refRectB;
intersection = SDL_IntersectRect(&rectA, &rectB, &result);
_validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);

empty = (SDL_bool)SDL_RectEmpty(&result);
SDLTest_AssertCheck(empty == SDL_TRUE, "Validate result is empty Rect; got: %s", (empty == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");

// Rect B empty
result.w = SDLTest_RandomIntegerInRange(1, 100);
result.h = SDLTest_RandomIntegerInRange(1, 100);
refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
Expand All @@ -640,8 +647,12 @@ int rect_testIntersectRectEmpty (void *arg)
rectB = refRectB;
intersection = SDL_IntersectRect(&rectA, &rectB, &result);
_validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
empty = (SDL_bool)SDL_RectEmpty(&result);
SDLTest_AssertCheck(empty == SDL_TRUE, "Validate result is empty Rect; got: %s", (empty == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");

// Rect A and B empty
result.w = SDLTest_RandomIntegerInRange(1, 100);
result.h = SDLTest_RandomIntegerInRange(1, 100);
refRectA.x = SDLTest_RandomIntegerInRange(1, 100);
refRectA.y = SDLTest_RandomIntegerInRange(1, 100);
refRectA.w = SDLTest_RandomIntegerInRange(1, 100);
Expand All @@ -655,8 +666,10 @@ int rect_testIntersectRectEmpty (void *arg)
rectB = refRectB;
intersection = SDL_IntersectRect(&rectA, &rectB, &result);
_validateIntersectRectResults(intersection, SDL_FALSE, &rectA, &rectB, &refRectA, &refRectB, (SDL_Rect *)NULL, (SDL_Rect *)NULL);
empty = (SDL_bool)SDL_RectEmpty(&result);
SDLTest_AssertCheck(empty == SDL_TRUE, "Validate result is empty Rect; got: %s", (empty == SDL_TRUE) ? "SDL_TRUE" : "SDL_FALSE");

return TEST_COMPLETED;
return TEST_COMPLETED;
}

/*!
Expand Down

0 comments on commit 5239c67

Please sign in to comment.