From 8bf2e098f5f1e9c28f8e5671b9ba57f2079ed954 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 12 Jun 2019 07:51:10 +0530 Subject: [PATCH] Fix an out of bounds read causing a crash when selecting text with the mouse in the alternate screen mode Fixes #1578 --- docs/changelog.rst | 7 +++++++ kitty/screen.c | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 8b008a397..39815e2a0 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -4,6 +4,13 @@ Changelog |kitty| is a feature full, cross-platform, *fast*, GPU based terminal emulator. To update |kitty|, :doc:`follow the instructions `. +0.14.3 [future] +--------------------- + +- Fix an out of bounds read causing a crash when selecting text with the mouse + in the alternate screen mode (:iss:`1578`) + + 0.14.2 [2019-06-09] --------------------- diff --git a/kitty/screen.c b/kitty/screen.c index 50ff86563..6e7de7a34 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -1580,6 +1580,18 @@ range_line_(Screen *self, int y) { return self->linebuf->line; } +static inline int +clamp_for_range_line(Screen *self, int y) { + if (y < 0) { + unsigned int idx = -(y + 1); + if (idx >= self->historybuf->count) { + y += idx - self->historybuf->count + 1; + } + return y; + } + return MIN((unsigned int)y, self->lines - 1); +} + #define iterate_over_rectangle(start, end, line_func, y_type) { \ y_type min_y = MIN(start->y, end->y), max_y = MAX(start->y, end->y); \ index_type min_x = MIN(start->x, end->x), max_x = MAX(start->x, end->x); \ @@ -1982,6 +1994,8 @@ text_for_selection(Screen *self, PyObject *a UNUSED) { FullSelectionBoundary start, end; full_selection_limits_(selection, &start, &end); PyObject *ans = NULL; + start.y = clamp_for_range_line(self, start.y); + end.y = clamp_for_range_line(self, end.y); if (start.y == end.y && start.x == end.x) ans = PyTuple_New(0); else text_for_range(ans, start, end, self->selection.rectangle_select, true, range_line_, int); return ans;