diff --git a/kitty/history.c b/kitty/history.c index ab3d9d21c..a530d4c92 100644 --- a/kitty/history.c +++ b/kitty/history.c @@ -522,6 +522,7 @@ push(HistoryBuf *self, PyObject *args) { #define push_doc "Push a line into this buffer, removing the oldest line, if necessary" Line *line; if (!PyArg_ParseTuple(args, "O!", &Line_Type, &line)) return NULL; + if (!line_styles_are_valid(line)) return NULL; ANSIBuf as_ansi_buf = {0}; historybuf_add_line(self, line, &as_ansi_buf); free(as_ansi_buf.buf); diff --git a/kitty/line-buf.c b/kitty/line-buf.c index a49e771e1..d006d39c6 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -289,6 +289,7 @@ create_line_copy_inner(LineBuf *self, index_type y) { line->attrs = self->line_attrs[y]; init_line(self, &src, self->line_map[y]); copy_line(&src, line); + line->style_generation = tc_style_table(self->text_cache)->generation; return (PyObject *)line; } @@ -323,6 +324,8 @@ copy_line_to(LineBuf *self, PyObject *args) { copy_line(&src, dest); if (self->text_cache != dest->text_cache) remap_style_idx_range(tc_style_table(dest->text_cache), tc_style_table(self->text_cache), dest->cpu_cells, dest->xnum); + // all the cells of dest now have current style indices + if (dest->style_generation) dest->style_generation = tc_style_table(dest->text_cache)->generation; Py_RETURN_NONE; } diff --git a/kitty/line.c b/kitty/line.c index fb75b6a3a..4b184b76f 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -673,6 +673,7 @@ line_as_ansi(Line *self, ANSILineState *s, index_type start_at, index_type stop_ static PyObject * as_ansi(Line *self, PyObject *a UNUSED) { #define as_ansi_doc "Return the line's contents with ANSI (SGR) escape codes for formatting" + if (!line_styles_are_valid(self)) return NULL; ANSIBuf output = {0}; ANSILineState s = {.output_buf = &output}; line_as_ansi(self, &s, 0, self->xnum, 0, true); @@ -759,6 +760,7 @@ set_text(Line *self, PyObject *args) { void *buf; if (!PyArg_ParseTuple(args, "UnnO!", &src, &offset, &sz, &Cursor_Type, &cursor)) return NULL; + if (!line_styles_are_valid(self)) return NULL; if (PyUnicode_READY(src) != 0) { PyErr_NoMemory(); return NULL; @@ -788,6 +790,7 @@ cursor_from(Line *self, PyObject *args) { unsigned int x, y = 0; Cursor *ans; if (!PyArg_ParseTuple(args, "I|I", &x, &y)) return NULL; + if (!line_styles_are_valid(self)) return NULL; if (x >= self->xnum) { PyErr_SetString(PyExc_ValueError, "Out of bounds x"); return NULL; @@ -843,6 +846,7 @@ apply_cursor(Line *self, PyObject *args) { unsigned int at = 0, num = 1; int clear_char = 0; if (!PyArg_ParseTuple(args, "O!|IIp", &Cursor_Type, &cursor, &at, &num, &clear_char)) return NULL; + if (!line_styles_are_valid(self)) return NULL; line_apply_cursor(self, cursor, at, num, clear_char & 1); Py_RETURN_NONE; } @@ -909,6 +913,7 @@ set_char(Line *self, PyObject *args) { unsigned int hyperlink_id = 0; if (!PyArg_ParseTuple(args, "IC|IO!I", &at, &ch, &width, &Cursor_Type, &cursor, &hyperlink_id)) return NULL; + if (!line_styles_are_valid(self)) return NULL; if (at >= self->xnum) { PyErr_SetString(PyExc_ValueError, "Out of bounds"); return NULL; @@ -927,6 +932,7 @@ set_attribute(Line *self, PyObject *args) { unsigned int val; char *which; if (!PyArg_ParseTuple(args, "sI", &which, &val)) return NULL; + if (!line_styles_are_valid(self)) return NULL; if (!set_named_attribute_on_line(line_style_table(self), self->cpu_cells, self->render_cells, which, val, self->xnum)) { PyErr_SetString(PyExc_KeyError, "Unknown cell attribute"); return NULL; @@ -1238,6 +1244,7 @@ copy_char(Line *self, PyObject *args) { unsigned int src, dest; Line *to; if (!PyArg_ParseTuple(args, "IO!I", &src, &Line_Type, &to, &dest)) return NULL; + if (!line_styles_are_valid(self) || !line_styles_are_valid(to)) return NULL; if (src >= self->xnum || dest >= to->xnum) { PyErr_SetString(PyExc_ValueError, "Out of bounds"); return NULL; diff --git a/kitty/line.h b/kitty/line.h index 2c97ba903..6a1c37ae9 100644 --- a/kitty/line.h +++ b/kitty/line.h @@ -111,6 +111,10 @@ typedef struct { bool needs_free; LineAttrs attrs; TextCache *text_cache; + // For lines that are copies, the generation of the style table when the + // copy was made, zero for lines that are views into a buffer. The style + // indices in a copy are invalid after the style table is garbage collected. + uint32_t style_generation; } Line; typedef struct MultiCellCommand { @@ -237,6 +241,16 @@ line_style_table(const Line *l) { return tc_style_table(l->text_cache); } +static inline bool +line_styles_are_valid(const Line *l) { + // Sets a Python exception and returns false if l is a copy made before the style table was garbage collected + if (l->style_generation && l->style_generation != line_style_table(l)->generation) { + PyErr_SetString(PyExc_ValueError, "The formatting of this copy of a line is no longer valid, as the style table changed after it was copied"); + return false; + } + return true; +} + static inline const Style * line_cell_style(const Line *l, index_type x) { return style_table_get(line_style_table(l), l->cpu_cells[x].style_idx); diff --git a/kitty_tests/styles.py b/kitty_tests/styles.py index 9dc294b9e..5a614317f 100644 --- a/kitty_tests/styles.py +++ b/kitty_tests/styles.py @@ -360,3 +360,22 @@ class TestStyles(BaseTest): s.draw('d') self.assertTrue(s.line(0).cursor_from(0).bold) self.assert_styles_consistent(s) + + def test_style_of_line_copies_after_gc(self): + s = self.create_screen(cols=6, lines=3, scrollback=2) + parse_bytes(s, RED_BOLD) + s.draw('abc') + copy = s.linebuf.create_line_copy(0) + self.assertTrue(copy.cursor_from(0).bold) + view = s.line(0) + s.test_gc_style_table() + # Views into the screen are remapped by the collection + self.assertTrue(view.cursor_from(0).bold) + # Copies are not, so using their formatting is an error + for f in (lambda: copy.cursor_from(0), copy.as_ansi, lambda: copy.apply_cursor(Cursor(), 0, 1), lambda: copy.set_attribute('bold', 1)): + self.assertRaises(ValueError, f) + self.ae(str(copy), 'abc') # the text is still usable + # Copying a whole line into the copy makes its formatting valid again + s.linebuf.copy_line_to(0, copy) + self.assertTrue(copy.cursor_from(0).bold) + self.assert_styles_consistent(s)