This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-10-quiet-head
in repository terminology.

View the commit online.

commit 18c4684076cda3f3d713e337f871bc27f05bbc0f
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 8 22:13:19 2026 -0600

    termtymux: keep the shadow screen buffers on a shrink
    
    On a shrink, termtymux_frame_apply() reallocated screen, then screen2.
    If the second realloc failed, screen had already shrunk but ty->w and
    ty->h kept the larger size, so later code indexed past the buffer.
    
    Leave the buffers alone on a shrink, so they always hold at least
    w * h cells; a grow still resizes them before the size changes. The
    extra memory is released with the shadow pty. No capacity is tracked:
    termpty_resize() replaces the buffers with exact-size ones, so such a
    field would go stale.
---
 src/bin/termtymux.c  | 60 ++++++++++++++++++++++++++++++++++++++++++----------
 src/bin/tytest.c     |  1 +
 src/bin/unit_tests.h |  1 +
 3 files changed, 51 insertions(+), 11 deletions(-)

diff --git a/src/bin/termtymux.c b/src/bin/termtymux.c
index 76077dc0..844af4d8 100644
--- a/src/bin/termtymux.c
+++ b/src/bin/termtymux.c
@@ -26,20 +26,25 @@ termtymux_frame_apply(Termpty *ty, const TermSrvFrame *frame,
 
    if ((int)frame->cols != ty->w || (int)frame->rows != ty->h)
      {
+        size_t old_sz = (size_t)(ty->w * ty->h) * sizeof(Termcell);
         size_t new_sz = (size_t)(frame->cols * frame->rows) * sizeof(Termcell);
-        Termcell *s1, *s2;
 
-        /* Realloc sequentially: screen is updated first so that, if screen2
-         * fails, ty keeps a valid screen buffer at least frame->cols *
-         * frame->rows cells wide - both buffers always cover ty->w * ty->h
-         * once this function returns EINA_TRUE. */
-        s1 = realloc(ty->screen, new_sz);
-        if (!s1) return EINA_FALSE;
-        ty->screen = s1;
+        /* Both buffers always hold at least ty->w * ty->h cells: a shrink
+         * leaves them alone, and a grow resizes screen then screen2 before
+         * ty->w and ty->h change, so a failed realloc leaves them valid for
+         * the old size. */
+        if (new_sz > old_sz)
+          {
+             Termcell *s1, *s2;
 
-        s2 = realloc(ty->screen2, new_sz);
-        if (!s2) return EINA_FALSE;
-        ty->screen2 = s2;
+             s1 = realloc(ty->screen, new_sz);
+             if (!s1) return EINA_FALSE;
+             ty->screen = s1;
+
+             s2 = realloc(ty->screen2, new_sz);
+             if (!s2) return EINA_FALSE;
+             ty->screen2 = s2;
+          }
 
         ty->w = (int)frame->cols;
         ty->h = (int)frame->rows;
@@ -535,6 +540,39 @@ tytest_termtymux_frame_apply_resize(void)
    return 0;
 }
 
+/* Test: a shrink keeps the buffers, and growing back keeps the cells of
+ * every frame. */
+int
+tytest_termtymux_frame_apply_shrink_grow(void)
+{
+   Termpty *ty = _tt_test_pty_new(4, 4);
+   TermSrvCell cells[16];
+   TermSrvFrame small = { .cols = 2, .rows = 2, .cursor_visible = 1 };
+   TermSrvFrame big = { .cols = 4, .rows = 4, .cursor_visible = 1 };
+   Termcell *screen, *screen2;
+   int i;
+
+   assert(ty);
+   memset(cells, 0, sizeof(cells));
+   for (i = 0; i < 16; i++) cells[i].codepoint = 'a' + i;
+
+   screen = ty->screen;
+   screen2 = ty->screen2;
+   assert(termtymux_frame_apply(ty, &small, cells));
+   assert(ty->w == 2 && ty->h == 2);
+   assert(ty->screen == screen && ty->screen2 == screen2);
+   for (i = 0; i < 4; i++)
+     assert(ty->screen[i].codepoint == (Eina_Unicode)('a' + i));
+
+   assert(termtymux_frame_apply(ty, &big, cells));
+   assert(ty->w == 4 && ty->h == 4);
+   for (i = 0; i < 16; i++)
+     assert(ty->screen[i].codepoint == (Eina_Unicode)('a' + i));
+
+   termpty_free(ty);
+   return 0;
+}
+
 /* Test: termtymux_frame_read() grows the buffer to the frame, starting from
  * none, and keeps it when the frame fits. */
 int
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 0039d83c..12489aa4 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -168,6 +168,7 @@ static struct {
        { "termsrv_cell_layout", tytest_termsrv_cell_layout},
        { "termtymux_frame_apply_same_size", tytest_termtymux_frame_apply_same_size},
        { "termtymux_frame_apply_resize", tytest_termtymux_frame_apply_resize},
+       { "termtymux_frame_apply_shrink_grow", tytest_termtymux_frame_apply_shrink_grow},
        { "termtymux_frame_read_grow", tytest_termtymux_frame_read_grow},
        { "termtymux_frame_apply_invalid", tytest_termtymux_frame_apply_invalid},
        { "termtymux_modes_apply", tytest_termtymux_modes_apply},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index da7c3f21..d434b268 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -126,6 +126,7 @@ int tytest_termsrv_attached(void);
 int tytest_termsrv_cell_layout(void);
 int tytest_termtymux_frame_apply_same_size(void);
 int tytest_termtymux_frame_apply_resize(void);
+int tytest_termtymux_frame_apply_shrink_grow(void);
 int tytest_termtymux_frame_read_grow(void);
 int tytest_termtymux_frame_apply_invalid(void);
 int tytest_termtymux_modes_apply(void);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to