From 1ff75a5c0c182beb3c15bf3a44298ef6d449b1d2 Mon Sep 17 00:00:00 2001 From: Bill Kendrick Date: Mon, 15 Nov 2021 20:40:26 -0800 Subject: [PATCH] Stop recursive flood fill at depth of 20K Attempt to void crashing (by blowing up the stack) when doing a flood-fill of a complicated shape on a large canvas (e.g., `tuxpaint --3000x2000` with `starters/mosaic.svg`). --- docs/CHANGES.txt | 5 +++++ src/fill.c | 16 ++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/docs/CHANGES.txt b/docs/CHANGES.txt index feed8c26b..2d4a0dae8 100644 --- a/docs/CHANGES.txt +++ b/docs/CHANGES.txt @@ -192,6 +192,11 @@ $Id$ (e.g., when returning from the "Open" dialog). (Closes https://sourceforge.net/p/tuxpaint/feature-requests/186/) + * Attempt to void crashing (by blowing up the stack) when doing + a flood-fill of a complicated shape on a large canvas + (e.g., `tuxpaint --3000x2000` with `starters/mosaic.svg`). + (h/t Yang for reporting, and Pere for confirming) + * Ports & Building ---------------- * Fix compilation error on Linux with HOST environment variable set. diff --git a/src/fill.c b/src/fill.c index c2e68e6d7..5664a4869 100644 --- a/src/fill.c +++ b/src/fill.c @@ -27,7 +27,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA (See COPYING.txt) - Last updated: October 24, 2021 + Last updated: November 15, 2021 $Id$ */ @@ -64,7 +64,7 @@ double colors_close(SDL_Surface * canvas, Uint32 c1, Uint32 c2); Uint32 blend(SDL_Surface * canvas, Uint32 draw_colr, Uint32 old_colr, double pct); -void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Surface * last, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr, int * x1, int * y1, int * x2, int * y2, Uint8 * touched, int y_outside); +void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Surface * last, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr, int * x1, int * y1, int * x2, int * y2, Uint8 * touched, int y_outside, Uint32 cnt); void draw_brush_fill_single(SDL_Surface * canvas, int x, int y, Uint32 draw_color, Uint8 * touched); @@ -133,10 +133,10 @@ Uint32 blend(SDL_Surface * canvas, Uint32 draw_colr, Uint32 old_colr, double pct } void simulate_flood_fill(SDL_Surface * screen, SDL_Surface * last, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr, int * x1, int * y1, int * x2, int * y2, Uint8 * touched) { - simulate_flood_fill_outside_check(screen, last, canvas, x, y, cur_colr, old_colr, x1, y1, x2, y2, touched, 0); + simulate_flood_fill_outside_check(screen, last, canvas, x, y, cur_colr, old_colr, x1, y1, x2, y2, touched, 0, 0); } -void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Surface * last, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr, int * x1, int * y1, int * x2, int * y2, Uint8 * touched, int y_outside) +void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Surface * last, SDL_Surface * canvas, int x, int y, Uint32 cur_colr, Uint32 old_colr, int * x1, int * y1, int * x2, int * y2, Uint8 * touched, int y_outside, Uint32 cnt) { int fillL, fillR, narrowFillL, narrowFillR, i, outside; double in_line, closeness; @@ -144,6 +144,10 @@ void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Surface * last, Uint32 px_colr; Uint8 touch_byt; + /* Don't blow up the stack! */ + /* FIXME: Would be better to do this a more reliable way */ + if (cnt >= 20000) + return; /* "Same" color? No need to fill */ if (!would_flood_fill(canvas, cur_colr, old_colr)) @@ -313,7 +317,7 @@ void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Surface * last, ) ) { - simulate_flood_fill_outside_check(screen, last, canvas, i, y - 1, cur_colr, old_colr, x1, y1, x2, y2, touched, y_outside + 1); + simulate_flood_fill_outside_check(screen, last, canvas, i, y - 1, cur_colr, old_colr, x1, y1, x2, y2, touched, y_outside + 1, cnt + 1); } px_colr = getpixels[last->format->BytesPerPixel] (last, i, y + 1); @@ -325,7 +329,7 @@ void simulate_flood_fill_outside_check(SDL_Surface * screen, SDL_Surface * last, ) ) { - simulate_flood_fill_outside_check(screen, last, canvas, i, y + 1, cur_colr, old_colr, x1, y1, x2, y2, touched, y_outside + 1); + simulate_flood_fill_outside_check(screen, last, canvas, i, y + 1, cur_colr, old_colr, x1, y1, x2, y2, touched, y_outside + 1, cnt + 1); } } }