From 7f309d69ef1712f9453344d99ea2dfc59ec76fd1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Fri, 9 Feb 2024 02:04:43 -0500
Subject: [PATCH] Fix crash found by vttest.

If DL was passed a parameter equal to or greater than the number of
lines from the current line to the last line in the region, it would
try to move a negative-sized box up, which resulted in a crash.

This fixes that three ways:
1) Do not allow moving boxes with negative X/Y sizes
2) In dellines, clamp the lines to the number that could be deleted
3) Don't move lines up of all rest of the lines are being deleted
---
 src/conio/bitmap_con.c | 2 ++
 src/conio/cterm.c      | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/conio/bitmap_con.c b/src/conio/bitmap_con.c
index 20102f8274..f6eb9b82a2 100644
--- a/src/conio/bitmap_con.c
+++ b/src/conio/bitmap_con.c
@@ -1215,6 +1215,8 @@ int bitmap_movetext(int x, int y, int ex, int ey, int tox, int toy)
 			|| ey>cio_textinfo.screenheight
 			|| toy>cio_textinfo.screenheight
 			|| (toy + height - 1) > cio_textinfo.screenheight
+			|| ex < x
+			|| ey < y
 			) {
 		return(0);
 	}
diff --git a/src/conio/cterm.c b/src/conio/cterm.c
index 7238973c4e..7c52c68ec4 100644
--- a/src/conio/cterm.c
+++ b/src/conio/cterm.c
@@ -1231,7 +1231,10 @@ dellines(struct cterminal * cterm, int lines)
 	if (x < TERM_MINX || x > TERM_MAXX || y < TERM_MINY || y > TERM_MAXY)
 		return;
 	SCR_XY(&sx, &sy);
-	MOVETEXT(minx, sy + lines, maxx, maxy, minx, sy);
+	if ((sy + lines - 1) > maxy) // Delete all the lines... ie: clear screen...
+		lines = maxy - sy + 1;
+	if (sy + lines <= maxy)
+		MOVETEXT(minx, sy + lines, maxx, maxy, minx, sy);
 	for(i = TERM_MAXY - lines + 1; i <= TERM_MAXY; i++) {
 		cterm_gotoxy(cterm, TERM_MINX, i);
 		cterm_clreol(cterm);
-- 
GitLab