Skip to content

Fix draw halting when using NaN argument #3467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 71 additions & 46 deletions src_c/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ draw_round_rect(SDL_Surface *surf, SDL_Rect surf_clip_rect, int x1, int y1,
return NULL; \
}

#define CHECK_NAN(value) ((unsigned int)value == 0x80000000)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have something like isnan from math.h instead of this


/* Definition of functions that get called in Python */

/* Draws an antialiased line on the given surface.
Expand Down Expand Up @@ -183,6 +185,10 @@ aaline(PyObject *self, PyObject *arg, PyObject *kwargs)
return pgRect_New4((int)startx, (int)starty, 0, 0);
}

if (CHECK_NAN(startx)) {
return pgRect_New4((int)startx, (int)starty, 0, 0);
}

if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}
Expand Down Expand Up @@ -270,6 +276,11 @@ line(PyObject *self, PyObject *arg, PyObject *kwargs)
return pgRect_New4(startx, starty, 0, 0);
}

if (CHECK_NAN(startx) || CHECK_NAN(starty) || CHECK_NAN(endx) ||
CHECK_NAN(endy)) {
return pgRect_New4(startx, starty, 0, 0);
}

if (!pgSurface_Lock(surfobj)) {
return RAISE(PyExc_RuntimeError, "error locking surface");
}
Expand Down Expand Up @@ -421,15 +432,17 @@ aalines(PyObject *self, PyObject *arg, PyObject *kwargs)
extra_px = steep_prev > steep_curr;
disable_endpoints =
!((roundf(pts[2]) == pts[2]) && (roundf(pts[3]) == pts[3]));
if (closed) {
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0], pts[1],
pts[2], pts[3], drawn_area, disable_endpoints,
disable_endpoints, extra_px);
}
else {
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0], pts[1],
pts[2], pts[3], drawn_area, 0, disable_endpoints,
extra_px);
if (!CHECK_NAN(pts[0])) {
if (closed) {
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0],
pts[1], pts[2], pts[3], drawn_area, disable_endpoints,
disable_endpoints, extra_px);
}
else {
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0],
pts[1], pts[2], pts[3], drawn_area, 0,
disable_endpoints, extra_px);
}
}

for (loop = 2; loop < length - 1; ++loop) {
Expand All @@ -440,19 +453,21 @@ aalines(PyObject *self, PyObject *arg, PyObject *kwargs)

/* Comparing previous and current line.
* If one is steep and other is not, extra pixel must be drawn.*/
steep_prev =
fabs(pts_prev[2] - pts_prev[0]) < fabs(pts_prev[3] - pts_prev[1]);
steep_curr = fabs(pts[2] - pts[0]) < fabs(pts[3] - pts[1]);
extra_px = steep_prev != steep_curr;
disable_endpoints =
!((roundf(pts[2]) == pts[2]) && (roundf(pts[3]) == pts[3]));
pts_prev[0] = pts[0];
pts_prev[1] = pts[1];
pts_prev[2] = pts[2];
pts_prev[3] = pts[3];
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0], pts[1],
pts[2], pts[3], drawn_area, disable_endpoints,
disable_endpoints, extra_px);
if (!CHECK_NAN(pts[0])) {
steep_prev = fabs(pts_prev[2] - pts_prev[0]) <
fabs(pts_prev[3] - pts_prev[1]);
steep_curr = fabs(pts[2] - pts[0]) < fabs(pts[3] - pts[1]);
extra_px = steep_prev != steep_curr;
disable_endpoints =
!((roundf(pts[2]) == pts[2]) && (roundf(pts[3]) == pts[3]));
pts_prev[0] = pts[0];
pts_prev[1] = pts[1];
pts_prev[2] = pts[2];
pts_prev[3] = pts[3];
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0],
pts[1], pts[2], pts[3], drawn_area, disable_endpoints,
disable_endpoints, extra_px);
}
}

/* Last line - if open, add endpoint pixels. */
Expand All @@ -470,31 +485,35 @@ aalines(PyObject *self, PyObject *arg, PyObject *kwargs)
pts_prev[1] = pts[1];
pts_prev[2] = pts[2];
pts_prev[3] = pts[3];
if (closed) {
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0], pts[1],
pts[2], pts[3], drawn_area, disable_endpoints,
disable_endpoints, extra_px);
}
else {
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0], pts[1],
pts[2], pts[3], drawn_area, disable_endpoints, 0,
extra_px);
if (!CHECK_NAN(pts[0])) {
if (closed) {
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0],
pts[1], pts[2], pts[3], drawn_area, disable_endpoints,
disable_endpoints, extra_px);
}
else {
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0],
pts[1], pts[2], pts[3], drawn_area, disable_endpoints,
0, extra_px);
}
}

if (closed && length > 2) {
pts[0] = xlist[length - 1];
pts[1] = ylist[length - 1];
pts[2] = xlist[0];
pts[3] = ylist[0];
steep_prev =
fabs(pts_prev[2] - pts_prev[0]) < fabs(pts_prev[3] - pts_prev[1]);
steep_curr = fabs(pts[2] - pts[0]) < fabs(pts[3] - pts[1]);
extra_px = steep_prev != steep_curr;
disable_endpoints =
!((roundf(pts[2]) == pts[2]) && (roundf(pts[3]) == pts[3]));
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0], pts[1],
pts[2], pts[3], drawn_area, disable_endpoints,
disable_endpoints, extra_px);
if (!CHECK_NAN(pts[0])) {
steep_prev = fabs(pts_prev[2] - pts_prev[0]) <
fabs(pts_prev[3] - pts_prev[1]);
steep_curr = fabs(pts[2] - pts[0]) < fabs(pts[3] - pts[1]);
extra_px = steep_prev != steep_curr;
disable_endpoints =
!((roundf(pts[2]) == pts[2]) && (roundf(pts[3]) == pts[3]));
draw_aaline(surf, surf_clip_rect, surf_format, color, pts[0],
pts[1], pts[2], pts[3], drawn_area, disable_endpoints,
disable_endpoints, extra_px);
}
}

PyMem_Free(points_buf);
Expand Down Expand Up @@ -615,15 +634,21 @@ lines(PyObject *self, PyObject *arg, PyObject *kwargs)
}

for (loop = 1; loop < length; ++loop) {
draw_line_width(surf, surf_clip_rect, color, xlist[loop - 1],
ylist[loop - 1], xlist[loop], ylist[loop], width,
drawn_area);
if (!CHECK_NAN(xlist[loop - 1]) && !CHECK_NAN(ylist[loop - 1]) &&
!CHECK_NAN(xlist[loop]) && !CHECK_NAN(ylist[loop])) {
draw_line_width(surf, surf_clip_rect, color, xlist[loop - 1],
ylist[loop - 1], xlist[loop], ylist[loop], width,
drawn_area);
}
}

if (closed && length > 2) {
draw_line_width(surf, surf_clip_rect, color, xlist[length - 1],
ylist[length - 1], xlist[0], ylist[0], width,
drawn_area);
if (!CHECK_NAN(xlist[length - 1]) && !CHECK_NAN(ylist[length - 1]) &&
!CHECK_NAN(xlist[0]) && !CHECK_NAN(ylist[0])) {
draw_line_width(surf, surf_clip_rect, color, xlist[length - 1],
ylist[length - 1], xlist[0], ylist[0], width,
drawn_area);
}
}

PyMem_Free(xlist);
Expand Down
Loading