This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch master
in repository terminology.
View the commit online.
commit 20c4df0372bb2c16b9f42a0c0749822a43e6998d
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 12:52:09 2026 -0600
backlog: give back the block references of a dropped row
Saving a row into the backlog copies its cells with TERMPTY_CELL_COPY,
which takes a reference on every link and block they show. Dropping
the row gave back only the link references. A block that had scrolled
into the backlog therefore kept a reference once its row was gone,
whether the backlog was cleared or the row fell out of the ring, and
stayed allocated until its id was reused.
Rows now give back block references along with link references,
through one take and release pair shared with the resize. The resize
must use it too when it pulls a wrapped row back: the row gives its
references back before the rewrap takes them again, so without the
take a block only that row showed would be freed in between. Freeing
a terminal drops its blocks before its backlog, so the block table is
cleared first and the backlog has nothing left to release.
---
src/bin/backlog.c | 26 ++-------
src/bin/termpty.c | 146 +++++++++++++++++++++++++++++++++---------------
src/bin/termpty.h | 49 ++++++++++++++++
src/bin/tytest.c | 2 +
src/bin/tytest_common.c | 2 +
src/bin/unit_tests.h | 2 +
6 files changed, 162 insertions(+), 65 deletions(-)
diff --git a/src/bin/backlog.c b/src/bin/backlog.c
index e769ee2a..e5b28d18 100644
--- a/src/bin/backlog.c
+++ b/src/bin/backlog.c
@@ -55,20 +55,6 @@ termpty_save_extract(Termsave *ts)
return ts;
}
-/* Release the link refcounts held by a row's contents. hl.size stays zero
- * until the first link is created, and most terminals never make one. */
-static void
-_ts_links_release(Termpty *ty, Termsave *ts)
-{
- unsigned int i;
-
- if (EINA_LIKELY(ty->hl.size == 0)) return;
- for (i = 0; i < ts->w; i++)
- {
- if (EINA_UNLIKELY(ts->cells[i].att.link_id))
- term_link_refcount_dec(ty, ts->cells[i].att.link_id, 1);
- }
-}
Termsave *
termpty_save_new(Termpty *ty, Termsave *ts, int w)
@@ -81,13 +67,11 @@ termpty_save_new(Termpty *ty, Termsave *ts, int w)
* scrollback times columns. */
if (ts->cells && !ts->comp && (ts->cap >= (unsigned int)w))
{
- _ts_links_release(ty, ts);
+ termpty_cells_refs_release(ty, ts->cells, ts->w);
/* Zeroing is only needed when the caller's copy does bookkeeping:
- * with hyperlinks, it stops TERMPTY_CELL_COPY from decrementing
- * the refcounts _ts_links_release() already released; with
- * blocks, it keeps stale block codepoints from firing the
- * overwrite handler. Otherwise the copy overwrites every cell
- * anyway and this would write the same bytes twice. */
+ * it stops TERMPTY_CELL_COPY from releasing again the link and
+ * block references just given back. Otherwise the copy overwrites
+ * every cell anyway and this would write the same bytes twice. */
if ((w > 0) && ((ty->hl.size != 0) || (ty->block.blocks != NULL)))
memset(ts->cells, 0, (size_t)w * sizeof(Termcell));
ts->w = w;
@@ -148,7 +132,7 @@ termpty_save_free(Termpty *ty, Termsave *ts)
if (ts->comp) ts_comp--;
else ts_uncomp--;
ts_freeops++;
- _ts_links_release(ty, ts);
+ termpty_cells_refs_release(ty, ts->cells, ts->w);
free(ts->cells);
ts->cells = NULL;
diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 7425ddce..0d473a1d 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -886,6 +886,8 @@ termpty_free(Termpty *ty)
free(ty->osc52_long.data);
EINA_LIST_FREE(ty->block.expecting, ex) free(ex);
if (ty->block.blocks) eina_hash_free(ty->block.blocks);
+ /* so that the backlog, freed below, has no block left to release */
+ ty->block.blocks = NULL;
if (ty->block.chid_map) eina_hash_free(ty->block.chid_map);
if (ty->block.active) eina_list_free(ty->block.active);
if (ty->fd >= 0)
@@ -1507,45 +1509,6 @@ _termpty_line_rewrap(Termpty *ty, Termcell *src_cells, int len,
}
}
-/* Take or give back the references n cells hold on their hyperlinks. */
-static void
-_cells_links_ref(Termpty *ty, const Termcell *cells, int n, Eina_Bool take)
-{
- int i, j;
-
- if (EINA_LIKELY(ty->hl.size == 0))
- return;
- for (i = 0; i < n; i = j)
- {
- uint16_t link_id = cells[i].att.link_id;
-
- j = i + 1;
- while ((j < n) && (j - i < UINT16_MAX) &&
- (cells[j].att.link_id == link_id))
- j++;
- if (!link_id)
- continue;
- if (take)
- term_link_refcount_inc(ty, link_id, j - i);
- else
- term_link_refcount_dec(ty, link_id, j - i);
- }
-}
-
-/* Give back the block and link references of n cells about to be freed. */
-static void
-_cells_refs_release(Termpty *ty, const Termcell *cells, int n)
-{
- int i;
-
- if (ty->block.blocks)
- {
- for (i = 0; i < n; i++)
- HANDLE_BLOCK_CODEPOINT_OVERWRITE(ty, cells[i].codepoint, 0);
- }
- _cells_links_ref(ty, cells, n, EINA_FALSE);
-}
-
static void
_backlog_remove_latest_nolock(Termpty *ty)
{
@@ -1637,7 +1600,7 @@ termpty_resize(Termpty *ty, int new_w, int new_h)
}
/* the copy took references for the cells it kept, the cells cut
* off give theirs back with the rest */
- _cells_refs_release(ty, old_screen2, old_w * old_h);
+ termpty_cells_refs_release(ty, old_screen2, old_w * old_h);
}
free(old_screen2);
@@ -1686,14 +1649,15 @@ termpty_resize(Termpty *ty, int new_w, int new_h)
len+= ts->w;
/* the entry gives its references back before the rewrap takes
- * them again: a link only it referenced would be freed */
- _cells_links_ref(ty, new_cells, len, EINA_TRUE);
+ * them again: a link or a block only it showed would be
+ * freed */
+ termpty_cells_refs_take(ty, new_cells, len);
_backlog_remove_latest_nolock(ty);
_termpty_line_rewrap(ty, new_cells, len, &new_si,
old_y == ty->cursor_state.cy);
- _cells_links_ref(ty, new_cells, len, EINA_FALSE);
+ termpty_cells_refs_release(ty, new_cells, len);
free(new_cells);
old_y = 1;
}
@@ -1711,7 +1675,7 @@ termpty_resize(Termpty *ty, int new_w, int new_h)
}
/* the rewrap took references for the cells it copied */
- _cells_refs_release(ty, ty->screen, old_w * old_h);
+ termpty_cells_refs_release(ty, ty->screen, old_w * old_h);
free(ty->screen);
ty->screen = new_screen;
@@ -4931,4 +4895,98 @@ tytest_resize_screen2_link_refs(void)
}
#endif
+#if defined(BINARY_TYTEST)
+/* A 1x1 block shown at the top left of a new 10x3 terminal. */
+static Termblock *
+_ty_block_shown(Termpty *ty, int *id)
+{
+ Termblock *blk;
+
+ _ty_test_init(ty, 10, 3);
+ blk = termpty_block_new(ty, 1, 1, "/img.png", NULL);
+ assert(blk);
+ *id = blk->id;
+ termpty_block_insert(ty, '#', blk);
+ ty->block.on = EINA_TRUE;
+ _ty_feed(ty, "#");
+ ty->block.on = EINA_FALSE;
+ assert(blk->refs == 1);
+ return blk;
+}
+
+/* A block that scrolls into the backlog keeps one reference per cell that
+ * shows it there: it goes with the backlog row, whether the backlog is
+ * cleared or the row falls out of the ring, and a wrapped row pulled back
+ * on resize does not count it twice. */
+int
+tytest_backlog_block_refs(void)
+{
+ Termblock *blk;
+ Termpty ty;
+ int id, i;
+
+ blk = _ty_block_shown(&ty, &id);
+ _ty_feed(&ty, "\r\n\r\n\r\n");
+ assert(termpty_backlog_length(&ty) == 1);
+ assert(blk->refs == 1);
+ termpty_clear_backlog(&ty);
+ assert(!termpty_block_get(&ty, id));
+ _ty_test_shutdown(&ty);
+ eina_hash_free(ty.block.blocks);
+
+ _ty_block_shown(&ty, &id);
+ for (i = 0; i < 120; i++)
+ _ty_feed(&ty, "\r\n");
+ assert(!termpty_block_get(&ty, id));
+ _ty_test_shutdown(&ty);
+ eina_hash_free(ty.block.blocks);
+
+ blk = _ty_block_shown(&ty, &id);
+ _ty_feed(&ty, "LLLLLLLLLabcdefghijklmnopqrstuvw");
+ assert(termpty_backlog_length(&ty) == 1);
+ assert(blk->refs == 1);
+ termpty_resize(&ty, 12, 4);
+ assert(termpty_backlog_length(&ty) == 0);
+ assert(termpty_block_get(&ty, id) == blk);
+ assert(blk->refs == 1);
+ _ty_test_shutdown(&ty);
+ eina_hash_free(ty.block.blocks);
+ return 0;
+}
+
+/* Freeing a terminal with a block in its backlog: the backlog gives its
+ * block references back after the block table is gone, which must not
+ * reach the freed table. Run in a child, as a crash is what fails. */
+int
+tytest_termpty_free_backlog_block(void)
+{
+ pid_t pid;
+ int status = 0;
+
+ pid = fork();
+ assert(pid >= 0);
+ if (pid == 0)
+ {
+ Config *config;
+ Termpty *ty;
+ int id;
+
+ ty = malloc(sizeof(*ty));
+ assert(ty);
+ _ty_block_shown(ty, &id);
+ _ty_feed(ty, "\r\n\r\n\r\n");
+ assert(termpty_backlog_length(ty) == 1);
+ config = ty->config;
+ termpty_free(ty);
+ config_del(config);
+ ecore_shutdown();
+ eina_shutdown();
+ _exit(0);
+ }
+ assert(waitpid(pid, &status, 0) == pid);
+ assert(WIFEXITED(status) && WEXITSTATUS(status) == 0);
+ return 0;
+}
+#endif
+
#endif /* BINARY_TYFUZZ || BINARY_TYTEST */
diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index 0f636211..9693f5c8 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -619,6 +619,55 @@ termpty_cells_copy_refs(Termpty *ty, const Termcell *src, const Termcell *dst,
termpty_cells_link_refs(ty, src, dst, n);
}
+/* Take or give back the block and link references n cells hold, as when
+ * cells are copied with memcpy() or freed. */
+static inline void
+termpty_cells_refs_adjust(Termpty *ty, const Termcell *cells, int n,
+ Eina_Bool take)
+{
+ int i, j;
+
+ if (ty->block.blocks)
+ {
+ for (i = 0; i < n; i++)
+ {
+ if (take)
+ HANDLE_BLOCK_CODEPOINT_OVERWRITE(ty, 0, cells[i].codepoint);
+ else
+ HANDLE_BLOCK_CODEPOINT_OVERWRITE(ty, cells[i].codepoint, 0);
+ }
+ }
+ if (EINA_LIKELY(ty->hl.size == 0))
+ return;
+ for (i = 0; i < n; i = j)
+ {
+ uint16_t link_id = cells[i].att.link_id;
+
+ j = i + 1;
+ while ((j < n) && (j - i < UINT16_MAX) &&
+ (cells[j].att.link_id == link_id))
+ j++;
+ if (!link_id)
+ continue;
+ if (take)
+ term_link_refcount_inc(ty, link_id, j - i);
+ else
+ term_link_refcount_dec(ty, link_id, j - i);
+ }
+}
+
+static inline void
+termpty_cells_refs_take(Termpty *ty, const Termcell *cells, int n)
+{
+ termpty_cells_refs_adjust(ty, cells, n, EINA_TRUE);
+}
+
+static inline void
+termpty_cells_refs_release(Termpty *ty, const Termcell *cells, int n)
+{
+ termpty_cells_refs_adjust(ty, cells, n, EINA_FALSE);
+}
+
/* As termpty_cells_copy_refs(), for overlapping ranges: blocks are walked
* so that a cell gains its new reference before losing its old one. */
static inline void
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index a789e9a0..640daad7 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -81,6 +81,8 @@ static struct {
{ "resize_screen_link_refs", tytest_resize_screen_link_refs},
{ "resize_screen_block_refs", tytest_resize_screen_block_refs},
{ "resize_screen2_link_refs", tytest_resize_screen2_link_refs},
+ { "backlog_block_refs", tytest_backlog_block_refs},
+ { "termpty_free_backlog_block", tytest_termpty_free_backlog_block},
{ "percent_decode", tytest_percent_decode},
{ "sgr_58_no_leak", tytest_sgr_58_no_leak},
{ "xmodkeys_set", tytest_xmodkeys_set},
diff --git a/src/bin/tytest_common.c b/src/bin/tytest_common.c
index 2566352e..1c258604 100644
--- a/src/bin/tytest_common.c
+++ b/src/bin/tytest_common.c
@@ -1060,6 +1060,8 @@ _termpty_release(Termpty *ty)
free(ty->osc52_long.data);
EINA_LIST_FREE(ty->block.expecting, ex) free(ex);
if (ty->block.blocks) eina_hash_free(ty->block.blocks);
+ /* so that the backlog, freed below, has no block left to release */
+ ty->block.blocks = NULL;
if (ty->block.chid_map) eina_hash_free(ty->block.chid_map);
if (ty->block.active) eina_list_free(ty->block.active);
termpty_sync_output_reset(ty);
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index d30a8ffc..4d8fae40 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -54,6 +54,8 @@ int tytest_resize_wrapped_link(void);
int tytest_resize_screen_link_refs(void);
int tytest_resize_screen_block_refs(void);
int tytest_resize_screen2_link_refs(void);
+int tytest_backlog_block_refs(void);
+int tytest_termpty_free_backlog_block(void);
int tytest_percent_decode(void);
int tytest_sgr_58_no_leak(void);
int tytest_xmodkeys_set(void);
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.