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 954ec8a1259fe9ab631c328aa2b59ae9d1c44889
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 12:33:39 2026 -0600

    termpty: keep the links of a wrapped line pulled back on resize
    
    On resize, the wrapped entry at the top of the backlog is taken back
    and rewrapped with the screen. Freeing the entry released the
    references its cells held on their hyperlinks before the rewrap took
    them again. A link that only that entry referenced, such as one in the
    part of a wrapped line that had scrolled off, dropped to no reference
    and was freed: the rewrapped cells then pointed at a freed link, with
    no URL to open and a slot the next OSC 8 could reuse.
    
    The copy being rewrapped now holds the references across the free.
---
 src/bin/termpty.c    | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/tytest.c     |  1 +
 src/bin/unit_tests.h |  1 +
 3 files changed, 69 insertions(+)

diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 185d1fed..31ec75b9 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -1507,6 +1507,31 @@ _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);
+     }
+}
+
 static void
 _backlog_remove_latest_nolock(Termpty *ty)
 {
@@ -1644,11 +1669,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);
              _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);
              free(new_cells);
              old_y = 1;
           }
@@ -4733,4 +4762,42 @@ tytest_links_reset(void)
 }
 #endif
 
+#if defined(BINARY_TYTEST)
+/* A resize that pulls a wrapped line back from the backlog keeps a
+ * hyperlink only that backlog entry referenced: the entry gives its
+ * references back before the rewrap takes them again. */
+int
+tytest_resize_wrapped_link(void)
+{
+   Termpty ty;
+   const Termcell *cells;
+   const Term_Link *link;
+   ssize_t w = 0;
+   uint16_t link_id;
+
+   _ty_test_init(&ty, 10, 3);
+   _ty_feed(&ty, "\033]8;;http://example.com\033\\LLLLLLLLLL"
+            "\033]8;;\033\\abcdefghijklmnopqrstuvw");
+   assert(termpty_backlog_length(&ty) == 1);
+   cells = termpty_cellrow_get(&ty, -1, &w);
+   assert(cells && (w == 10) && cells[9].att.autowrapped);
+   link_id = cells[0].att.link_id;
+   assert(link_id);
+   assert(ty.hl.links[link_id].refcount == 10);
+
+   termpty_resize(&ty, 12, 4);
+   assert(termpty_backlog_length(&ty) == 0);
+   assert(_ty_cell_cp(&ty, 0, 0) == 'L');
+   cells = termpty_cellrow_get(&ty, 0, &w);
+   assert(cells[0].att.link_id == link_id);
+   link = &ty.hl.links[link_id];
+   assert(link->refcount == 10);
+   assert(link->url && !strcmp(link->url, "http://example.com"));
+
+   _ty_test_shutdown(&ty);
+   free(ty.hl.links);
+   return 0;
+}
+#endif
+
 #endif /* BINARY_TYFUZZ || BINARY_TYTEST */
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index cdfc1c4e..f04474ca 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -77,6 +77,7 @@ static struct {
        { "termpty_read_error", tytest_termpty_read_error},
        { "osc8_no_leak", tytest_osc8_no_leak},
        { "links_reset", tytest_links_reset},
+       { "resize_wrapped_link", tytest_resize_wrapped_link},
        { "percent_decode", tytest_percent_decode},
        { "sgr_58_no_leak", tytest_sgr_58_no_leak},
        { "xmodkeys_set", tytest_xmodkeys_set},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index ee7d72c9..17bf942c 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -50,6 +50,7 @@ int tytest_hl_links_exhausted(void);
 int tytest_termpty_read_error(void);
 int tytest_osc8_no_leak(void);
 int tytest_links_reset(void);
+int tytest_resize_wrapped_link(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.

Reply via email to