From 8299606b2c4b707ef759eb97a7cd5e1e38d2f186 Mon Sep 17 00:00:00 2001
From: Mamoru TASAKA <mtasaka@fedoraproject.org>
Date: Thu, 2 Mar 2023 21:44:14 +0900
Subject: [PATCH] distort_reset: restrict radius by xgwa correctly

new_rnd_coo() sets st->xy_coo[k].x and y, which calculates
the reminder of division, and this requires
both `st->xgwa.width-2*st->radius` and `st->xgwa.height-2*st->radius`
must be positive.

Otherwise, st->xy_coo[k].x or y can be negative, then plain_draw() passes
negative value to x or y for st->draw_routine(), then in st->draw_rountine()
(actually this is fast_draw_32() or so) segfault can happen.

So restrict st->radius value correctly so that st->radius is
smaller than min { st->xgwa.height, st->xgwa.width } / 2.

- fix ternary operator result so that lesser value is chosen
- avoid zero division - if st->radius is exactly the half of the chosen value
  above, st->xgwa.width-2*st->radius (or height side) gets 0, which causes
  zero division, so choose lesser value than 1/2 - now chose 2/5.
---
 hacks/distort.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hacks/distort.c b/hacks/distort.c
index fd605f6..2795119 100644
--- a/hacks/distort.c
+++ b/hacks/distort.c
@@ -243,7 +243,7 @@ distort_reset (struct state *st)
      */
     {
       int max = (st->xgwa.width > st->xgwa.height
-                 ? st->xgwa.width : st->xgwa.height) / (2 * st->number);
+                 ? st->xgwa.height : st->xgwa.width) * 2 / (5 * st->number);
       if (st->radius > max) st->radius = max;
     }
 
-- 
2.39.2

