From b6b270b28fa92d5bedca397313b5370893fe0e3f Mon Sep 17 00:00:00 2001 From: erw7 Date: Tue, 28 Jul 2020 00:51:41 +0900 Subject: [PATCH] terminal: fix terminal attribute overflow fixes #11548 --- src/nvim/screen.c | 6 +++--- src/nvim/screen.h | 3 +++ src/nvim/terminal.c | 1 + test/functional/terminal/buffer_spec.lua | 9 +++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/nvim/screen.c b/src/nvim/screen.c index ba52f5b489..7bed747e9a 100644 --- a/src/nvim/screen.c +++ b/src/nvim/screen.c @@ -2804,8 +2804,8 @@ win_line ( off += col; } - // wont highlight after 1024 columns - int term_attrs[1024] = {0}; + // wont highlight after TERM_ATTRS_MAX columns + int term_attrs[TERM_ATTRS_MAX] = { 0 }; if (wp->w_buffer->terminal) { terminal_get_line_attributes(wp->w_buffer->terminal, wp, lnum, term_attrs); extra_check = true; @@ -4172,7 +4172,7 @@ win_line ( int n = wp->w_p_rl ? -1 : 1; while (col >= 0 && col < grid->Columns) { schar_from_ascii(linebuf_char[off], ' '); - linebuf_attr[off] = term_attrs[vcol]; + linebuf_attr[off] = vcol >= TERM_ATTRS_MAX ? 0 : term_attrs[vcol]; off += n; vcol += n; col += n; diff --git a/src/nvim/screen.h b/src/nvim/screen.h index 9267672cf1..5f339b9ae2 100644 --- a/src/nvim/screen.h +++ b/src/nvim/screen.h @@ -32,6 +32,9 @@ EXTERN ScreenGrid default_grid INIT(= SCREEN_GRID_INIT); #define DEFAULT_GRID_HANDLE 1 // handle for the default_grid +// Maximum columns for terminal highlight attributes +#define TERM_ATTRS_MAX 1024 + /// Status line click definition typedef struct { enum { diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 6a13341a89..52d3eef810 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -607,6 +607,7 @@ void terminal_get_line_attributes(Terminal *term, win_T *wp, int linenr, return; } + width = MIN(TERM_ATTRS_MAX, width); for (int col = 0; col < width; col++) { VTermScreenCell cell; bool color_valid = fetch_cell(term, row, col, &cell); diff --git a/test/functional/terminal/buffer_spec.lua b/test/functional/terminal/buffer_spec.lua index f79aceaddf..6372cd935e 100644 --- a/test/functional/terminal/buffer_spec.lua +++ b/test/functional/terminal/buffer_spec.lua @@ -276,3 +276,12 @@ describe('No heap-buffer-overflow when using', function() feed_command('bdelete!') end) end) + +describe('No heap-buffer-overflow when', function() + it('set nowrap and send long line #11548', function() + feed_command('set nowrap') + feed_command('autocmd TermOpen * startinsert') + feed_command('call feedkeys("4000ai\\:terminal!\\")') + eq(2, eval('1+1')) + end) +end)