summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-08-14 14:45:27 -0400
committerJune McEnroe <june@causal.agency>2019-08-14 14:45:27 -0400
commit7e4ef2fdb10a77d065d70eaeae0d3cc14c46b1df (patch)
tree334e5e49f4a520caf02430fb83ec3bf5d1b390a5
parentHandle state transitions just much better (diff)
downloadstream-7e4ef2fdb10a77d065d70eaeae0d3cc14c46b1df.tar.gz
stream-7e4ef2fdb10a77d065d70eaeae0d3cc14c46b1df.zip
Adjust bounds checks again
Allows pointers to one past the last column. Prevents weird scrolls.
We'll see how this does in fuzzing...
Diffstat (limited to '')
-rw-r--r--term.c20
1 files changed, 10 insertions, 10 deletions
diff --git a/term.c b/term.c
index 9910889..418fdf6 100644
--- a/term.c
+++ b/term.c
@@ -27,8 +27,6 @@
 #include "term.h"
 
 #define MIN(a, b) ((a) < (b) ? (a) : (b))
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-#define BOX(l, x, u) MIN(MAX((x), (l)), (u))
 
 static void unhandled(const char *format, ...) {
 	if (isatty(STDERR_FILENO)) return;
@@ -42,7 +40,7 @@ static void unhandled(const char *format, ...) {
 
 static struct Cell *cell(struct Term *term, uint y, uint x) {
 	assert(y < term->rows);
-	assert(x < term->cols);
+	assert(x <= term->cols);
 	return &term->cells[y * term->cols + x];
 }
 
@@ -58,6 +56,7 @@ static void move(struct Cell *dst, struct Cell *src, uint len) {
 }
 
 static void scrollUp(struct Term *term, uint top, uint n) {
+	if (!n || top >= term->scroll.bot) return;
 	move(
 		cell(term, top, 0),
 		cell(term, top + n, 0),
@@ -71,6 +70,7 @@ static void scrollUp(struct Term *term, uint top, uint n) {
 }
 
 static void scrollDown(struct Term *term, uint top, uint n) {
+	if (!n || top >= term->scroll.bot) return;
 	move(
 		cell(term, top + n, 0),
 		cell(term, top, 0),
@@ -152,18 +152,18 @@ ACTION(ech) {
 }
 
 ACTION(dch) {
-	uint n = BOX(1, P(0, 1), R - X);
+	uint n = MIN(P(0, 1), t->cols - X);
 	move(C(Y, X), C(Y, X + n), t->cols - X - n);
 	erase(t->style, C(Y, t->cols - n), C(Y, R));
 }
 ACTION(ich) {
-	uint n = BOX(1, P(0, 1), R - X);
+	uint n = MIN(P(0, 1), t->cols - X);
 	move(C(Y, X + n), C(Y, X), t->cols - X - n);
-	erase(t->style, C(Y, X), C(Y, X + n - 1));
+	erase(t->style, C(Y, X), C(Y, X + n) - 1);
 }
 
-ACTION(dl) { scrollUp(t, Y, BOX(1, P(0, 1), t->scroll.bot - Y)); }
-ACTION(il) { scrollDown(t, Y, BOX(1, P(0, 1), t->scroll.bot - Y)); }
+ACTION(dl) { scrollUp(t, Y, MIN(P(0, 1), t->scroll.bot - Y)); }
+ACTION(il) { scrollDown(t, Y, MIN(P(0, 1), t->scroll.bot - Y)); }
 
 ACTION(nl) {
 	if (Y == t->scroll.bot) {
@@ -180,10 +180,10 @@ ACTION(ri) {
 	}
 }
 ACTION(su) {
-	scrollUp(t, t->scroll.top, BOX(1, P(0, 1), t->scroll.bot - t->scroll.top));
+	scrollUp(t, t->scroll.top, MIN(P(0, 1), t->scroll.bot - t->scroll.top));
 }
 ACTION(sd) {
-	scrollDown(t, t->scroll.top, BOX(1, P(0, 1), t->scroll.bot - t->scroll.top));
+	scrollDown(t, t->scroll.top, MIN(P(0, 1), t->scroll.bot - t->scroll.top));
 }
 ACTION(decstbm) {
 	t->scroll.bot = MIN(P(1, t->rows) - 1, B);