aboutsummaryrefslogtreecommitdiff
path: root/render.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2023-03-28 16:31:24 +0000
committerDaniel Eklöf <daniel@ekloef.se>2023-03-30 14:13:18 +0000
commit7bc22862fa9536cb2f18b030209a95306e57360d (patch)
tree545c0dd74f435f15468f5dd18539859dcbb6bb21 /render.c
parentinput: (kitty kbd): the resulting UTF-8 string may translate to multiple UTF-32 codepoints (diff)
render: protect against integer underflow when calculating scroll area
When applying scroll damage, we calculate the affected region’s height (in pixels), by subtracting the number of rows to scroll, from the scrolling region, and finally multiply by the cell height. If the number of rows to scroll is very large, the subtraction may underflow, resulting in a very large height value instead of a negative one. This caused the check for "scrolling too many lines" to fail. That in turn resulted in an integer overflow when calculating the source offset into the rendered surface buffer, which typically triggered a segfault. This bug happened when there was continuous output in the terminal without any new frames being rendered. This caused a buildup of scroll damage, that triggered the underflow+overflow when we finally did render a new frame. For example, a compositor that doesn’t send any frame callbacks (for example because the terminal window is minimized, or on a different workspace/tag) would cause this. Closes #1305
Diffstat (limited to 'render.c')
-rw-r--r--render.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/render.c b/render.c
index 2aba3237..f16898b4 100644
--- a/render.c
+++ b/render.c
@@ -929,14 +929,19 @@ static void
grid_render_scroll(struct terminal *term, struct buffer *buf,
const struct damage *dmg)
{
- int height = (dmg->region.end - dmg->region.start - dmg->lines) * term->cell_height;
-
LOG_DBG(
"damage: SCROLL: %d-%d by %d lines",
dmg->region.start, dmg->region.end, dmg->lines);
- if (height <= 0)
+ const int region_size = dmg->region.end - dmg->region.start;
+
+ if (dmg->lines >= region_size) {
+ /* The entire scroll region will be scrolled out (i.e. replaced) */
return;
+ }
+
+ const int height = (region_size - dmg->lines) * term->cell_height;
+ xassert(height > 0);
#if TIME_SCROLL_DAMAGE
struct timespec start_time;
@@ -1037,14 +1042,19 @@ static void
grid_render_scroll_reverse(struct terminal *term, struct buffer *buf,
const struct damage *dmg)
{
- int height = (dmg->region.end - dmg->region.start - dmg->lines) * term->cell_height;
-
LOG_DBG(
"damage: SCROLL REVERSE: %d-%d by %d lines",
dmg->region.start, dmg->region.end, dmg->lines);
- if (height <= 0)
+ const int region_size = dmg->region.end - dmg->region.start;
+
+ if (dmg->lines >= region_size) {
+ /* The entire scroll region will be scrolled out (i.e. replaced) */
return;
+ }
+
+ const int height = (region_size - dmg->lines) * term->cell_height;
+ xassert(height > 0);
#if TIME_SCROLL_DAMAGE
struct timespec start_time;