Fix `CSI Ps M` deleting lines above cursor

Fixes #2984.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 006000b..3fc0456 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -80,6 +80,7 @@
 - Terminal going transparent during visual bell
 - Selection not being cleared when sending chars through a binding
 - Mouse protocols/encodings not being mutually exclusive within themselves
+- Escape `CSI Ps M` deleting lines above cursor when at the bottom of the viewport
 
 ### Removed
 
diff --git a/alacritty_terminal/src/term/mod.rs b/alacritty_terminal/src/term/mod.rs
index 063897c..f0f3894 100644
--- a/alacritty_terminal/src/term/mod.rs
+++ b/alacritty_terminal/src/term/mod.rs
@@ -1564,9 +1564,12 @@
 
     #[inline]
     fn delete_lines(&mut self, lines: Line) {
+        let origin = self.cursor.point.line;
+        let lines = min(self.lines() - origin, lines);
+
         trace!("Deleting {} lines", lines);
-        if self.scroll_region.contains(&self.cursor.point.line) {
-            let origin = self.cursor.point.line;
+
+        if lines.0 > 0 && self.scroll_region.contains(&self.cursor.point.line) {
             self.scroll_up_relative(origin, lines);
         }
     }