From e12e67fae2b9702b8ec5b2f7c29542d26550ab24 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 16 Nov 2021 05:43:51 -0500 Subject: input validation for vpa --- CHANGELOG.md | 1 + src/grid.rs | 7 +++++++ tests/csi.rs | 10 ++++++++++ tests/data/fixtures/absolute_movement.in | 1 + tests/data/fixtures/absolute_movement/10.json | 4 ++-- tests/data/fixtures/absolute_movement/10.typescript | 2 +- tests/data/fixtures/absolute_movement/4.json | 4 ++-- tests/data/fixtures/absolute_movement/4.typescript | 2 +- tests/data/fixtures/absolute_movement/5.json | 2 +- tests/data/fixtures/absolute_movement/5.typescript | 2 +- tests/data/fixtures/absolute_movement/6.json | 2 +- tests/data/fixtures/absolute_movement/6.typescript | 2 +- tests/data/fixtures/absolute_movement/7.json | 2 +- tests/data/fixtures/absolute_movement/7.typescript | 2 +- tests/data/fixtures/absolute_movement/8.json | 2 +- tests/data/fixtures/absolute_movement/8.typescript | 2 +- tests/data/fixtures/absolute_movement/9.typescript | 2 +- 17 files changed, 34 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a391fda..16d0168 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * `Screen::rows_formatted` now outputs correct escape codes in some edge cases at the beginning of a row when the previous row was wrapped +* VPA escape sequence can no longer position the cursor off the screen ## [0.12.0] - 2021-03-09 diff --git a/src/grid.rs b/src/grid.rs index f5d75b3..021a787 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -540,6 +540,7 @@ impl Grid { pub fn row_set(&mut self, i: u16) { self.pos.row = i; + self.row_clamp(); } pub fn col_inc(&mut self, count: u16) { @@ -599,6 +600,12 @@ impl Grid { } } + fn row_clamp(&mut self) { + if self.pos.row > self.size.rows - 1 { + self.pos.row = self.size.rows - 1; + } + } + fn col_clamp(&mut self) { if self.pos.col > self.size.cols - 1 { self.pos.col = self.size.cols - 1; diff --git a/tests/csi.rs b/tests/csi.rs index bc33594..7a409b6 100644 --- a/tests/csi.rs +++ b/tests/csi.rs @@ -5,6 +5,16 @@ fn absolute_movement() { helpers::fixture("absolute_movement"); } +#[test] +fn row_clamp() { + let mut vt = vt100::Parser::default(); + assert_eq!(vt.screen().cursor_position(), (0, 0)); + vt.process(b"\x1b[15d"); + assert_eq!(vt.screen().cursor_position(), (14, 0)); + vt.process(b"\x1b[150d"); + assert_eq!(vt.screen().cursor_position(), (23, 0)); +} + #[test] fn relative_movement() { helpers::fixture("relative_movement"); diff --git a/tests/data/fixtures/absolute_movement.in b/tests/data/fixtures/absolute_movement.in index b8221ab..ddfb4be 100644 --- a/tests/data/fixtures/absolute_movement.in +++ b/tests/data/fixtures/absolute_movement.in @@ -1,6 +1,7 @@ \x1b[10;10H \x1b[d \x1b[15d +\x1b[150d \x1b[H \x1b[8H \x1b[15G diff --git a/tests/data/fixtures/absolute_movement/10.json b/tests/data/fixtures/absolute_movement/10.json index d8db62c..bb83ae4 100644 --- a/tests/data/fixtures/absolute_movement/10.json +++ b/tests/data/fixtures/absolute_movement/10.json @@ -2,7 +2,7 @@ "contents": "", "cells": {}, "cursor_position": [ - 23, - 79 + 0, + 0 ] } \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/10.typescript b/tests/data/fixtures/absolute_movement/10.typescript index 0bb223c..155097b 100644 --- a/tests/data/fixtures/absolute_movement/10.typescript +++ b/tests/data/fixtures/absolute_movement/10.typescript @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/4.json b/tests/data/fixtures/absolute_movement/4.json index bb83ae4..41f8756 100644 --- a/tests/data/fixtures/absolute_movement/4.json +++ b/tests/data/fixtures/absolute_movement/4.json @@ -2,7 +2,7 @@ "contents": "", "cells": {}, "cursor_position": [ - 0, - 0 + 23, + 9 ] } \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/4.typescript b/tests/data/fixtures/absolute_movement/4.typescript index 79743fd..c534421 100644 --- a/tests/data/fixtures/absolute_movement/4.typescript +++ b/tests/data/fixtures/absolute_movement/4.typescript @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/5.json b/tests/data/fixtures/absolute_movement/5.json index 69777cc..bb83ae4 100644 --- a/tests/data/fixtures/absolute_movement/5.json +++ b/tests/data/fixtures/absolute_movement/5.json @@ -2,7 +2,7 @@ "contents": "", "cells": {}, "cursor_position": [ - 7, + 0, 0 ] } \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/5.typescript b/tests/data/fixtures/absolute_movement/5.typescript index 45f0cd0..79743fd 100644 --- a/tests/data/fixtures/absolute_movement/5.typescript +++ b/tests/data/fixtures/absolute_movement/5.typescript @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/6.json b/tests/data/fixtures/absolute_movement/6.json index cb39b6e..69777cc 100644 --- a/tests/data/fixtures/absolute_movement/6.json +++ b/tests/data/fixtures/absolute_movement/6.json @@ -3,6 +3,6 @@ "cells": {}, "cursor_position": [ 7, - 14 + 0 ] } \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/6.typescript b/tests/data/fixtures/absolute_movement/6.typescript index 03227f7..45f0cd0 100644 --- a/tests/data/fixtures/absolute_movement/6.typescript +++ b/tests/data/fixtures/absolute_movement/6.typescript @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/7.json b/tests/data/fixtures/absolute_movement/7.json index 69777cc..cb39b6e 100644 --- a/tests/data/fixtures/absolute_movement/7.json +++ b/tests/data/fixtures/absolute_movement/7.json @@ -3,6 +3,6 @@ "cells": {}, "cursor_position": [ 7, - 0 + 14 ] } \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/7.typescript b/tests/data/fixtures/absolute_movement/7.typescript index 257c040..03227f7 100644 --- a/tests/data/fixtures/absolute_movement/7.typescript +++ b/tests/data/fixtures/absolute_movement/7.typescript @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/8.json b/tests/data/fixtures/absolute_movement/8.json index bb83ae4..69777cc 100644 --- a/tests/data/fixtures/absolute_movement/8.json +++ b/tests/data/fixtures/absolute_movement/8.json @@ -2,7 +2,7 @@ "contents": "", "cells": {}, "cursor_position": [ - 0, + 7, 0 ] } \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/8.typescript b/tests/data/fixtures/absolute_movement/8.typescript index 314a127..257c040 100644 --- a/tests/data/fixtures/absolute_movement/8.typescript +++ b/tests/data/fixtures/absolute_movement/8.typescript @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/data/fixtures/absolute_movement/9.typescript b/tests/data/fixtures/absolute_movement/9.typescript index 155097b..314a127 100644 --- a/tests/data/fixtures/absolute_movement/9.typescript +++ b/tests/data/fixtures/absolute_movement/9.typescript @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file -- cgit v1.2.3-54-g00ecf