altos/draw: Add validation for line drawing
authorKeith Packard <keithp@keithp.com>
Mon, 27 Feb 2023 02:46:09 +0000 (18:46 -0800)
committerKeith Packard <keithp@keithp.com>
Mon, 27 Feb 2023 02:46:09 +0000 (18:46 -0800)
Just make sure it never draws outside the dest.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/draw/ao_line.c

index c6b49ee0d6cb1fd71ffe83f3b1369cedeeb15896..832a916c76aed9dbe1042f7a024febe071ae93c6 100644 (file)
@@ -54,8 +54,9 @@
  *     adjust_x = e / e1;
  */
 
-
-
+#ifdef VALIDATE
+#include <stdio.h>
+#endif
 
 static void
 ao_bres(const struct ao_bitmap *dst_bitmap,
@@ -88,9 +89,22 @@ ao_bres(const struct ao_bitmap       *dst_bitmap,
        while (len--) {
                /* clip each point */
 
+#ifdef VALIDATE
+               if (x1 < 0 || dst_bitmap->width <= x1) {
+                       printf("bad line x %d\n", x1);
+                       return;
+               }
+               if (y1 < 0 || dst_bitmap->height <= y1) {
+                       printf("bad line y %d\n", y1);
+                       return;
+               }
+#endif
                *dst = ao_do_mask_rrop(*dst, and, xor, mask);
 
                if (axis == X_AXIS) {
+#ifdef VALIDATE
+                       x1 += signdx;
+#endif
                        if (signdx < 0)
                                mask = ao_left(mask, 1);
                        else
@@ -101,13 +115,22 @@ ao_bres(const struct ao_bitmap    *dst_bitmap,
                        }
                        e += e1;
                        if (e >= 0) {
+#ifdef VALIDATE
+                               y1 += signdy;
+#endif
                                dst += stride;
                                e += e3;
                        }
                } else {
+#ifdef VALIDATE
+                       y1 += signdy;
+#endif
                        dst += stride;
                        e += e1;
                        if (e >= 0) {
+#ifdef VALIDATE
+                               x1 += signdx;
+#endif
                                if (signdx < 0)
                                        mask = ao_left(mask, 1);
                                else