From 5ca376b56c3a495c9772e546995485159da2e664 Mon Sep 17 00:00:00 2001
From: Ben Bridle <ben@derelict.engineering>
Date: Wed, 13 Nov 2024 10:53:52 +1300
Subject: Fix line drawing issue

An overflow error was causing the line drawing method to loop forever
any time a line of length 0x4000 (16384) or longer was drawn. The issue
was occurring because both e1 and dx (or dy) would have a value of at
least 0x4000, and so on lines 291/292 the sum would exceed 0x8000, the
maximum value of an i16. The value would then wrap and break the
assumptions of the line drawing algorithm, causing it to loop forever.

This was fixed by increasing the size of the affected types.
---
 src/devices/screen_device.rs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

(limited to 'src/devices')

diff --git a/src/devices/screen_device.rs b/src/devices/screen_device.rs
index 73cfcae..b4eae22 100644
--- a/src/devices/screen_device.rs
+++ b/src/devices/screen_device.rs
@@ -270,11 +270,11 @@ impl ScreenDevice {
         let x_end: i16 = self.vector.x as i16;
         let y_end: i16 = self.vector.y as i16;
 
-        let dx: i16 =  (x_end - x).abs();
-        let dy: i16 = -(y_end - y).abs();
+        let dx: i32 =  ((x_end as i32) - (x as i32)).abs();
+        let dy: i32 = -((y_end as i32) - (y as i32)).abs();
         let sx: i16 = if x < x_end { 1 } else { -1 };
         let sy: i16 = if y < y_end { 1 } else { -1 };
-        let mut e1: i16 = dx + dy;
+        let mut e1: i32 = dx + dy;
 
         if draw & 0x10 != 0 {
         // Draw 1-bit textured line.
-- 
cgit v1.2.3-70-g09d2