Hi,

Programs using libedit(3) crashe after the program's window size is
changed.  For example,

  1. Invoke ftp
     $ ftp
     ftp>
  2. Resize its window
  3. Enter "deb" + <Tab>

     => When the problem occurs, it crashes with a segmentation fault
     => The problem doesn't occur, it displays "debug"

The problem happens once in 5-30 times.

See the problem by gdb.

  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  0x0000162ce7bd3ff2 in re_update_line (el=0x162cfa59d800, 
      old=0x162c3e478e00 L"deb", '\xdfdfdfdf' <repeats 125 times><error: Cannot 
access memory at address 0x162c3e479000>, new=0x162c3854ae00 L"ftp> debug", i=0)
      at /home/yasuoka/src/lib/libedit/refresh.c:518
  518             while (*o)
  (gdb) bt
  #0  0x0000162ce7bd3ff2 in re_update_line (el=0x162cfa59d800, 
      old=0x162c3e478e00 L"deb", '\xdfdfdfdf' <repeats 125 times><error: Cannot 
access memory at address 0x162c3e479000>, new=0x162c3854ae00 L"ftp> debug", i=0)
      at /home/yasuoka/src/lib/libedit/refresh.c:518
  #1  0x0000162ce7bd3c99 in re_refresh (el=0x162cfa59d800) at 
/home/yasuoka/src/lib/libedit/refresh.c:298
  #2  0x0000162ce7beba1c in el_wgets (el=0x162cfa59d800, nread=0x7f7ffffe1c6c) 
at /home/yasuoka/src/lib/libedit/read.c:577
  #3  0x0000162ce7be700a in el_gets (el=0x162cfa59d800, nread=0x7f7ffffe1c6c) 
at /home/yasuoka/src/lib/libedit/eln.c:74
  #4  0x0000162a32a19c60 in ?? ()
  #5  0x0000162a32a19817 in ?? ()
  #6  0x0000162a32a0b142 in ?? ()
  #7  0x0000000000000000 in ?? ()
  (gdb) p el->el_display[0]
  $1 = 0x162c3e478e00 L"deb", '\xdfdfdfdf' <repeats 125 times><error: Cannot 
access memory at address 0x162c3e479000>
  (gdb) 

When <tab> is entered, as the result of the completion, libedit calls
re_update_line() <= re_reresh() to refresh the line.  In that
function, it tries to find a NUL char in the line buffer, but the line
buffer doesn't include any NUL, then the crash happens.

After the window size change, the line buffer is freeed and allocated
again at terminal_alloc_display().  (These are done by signal
handler.  This is a separated problem)

 409 static int
 410 terminal_alloc_display(EditLine *el)
 411 {
 412         int i;
 413         wchar_t **b;
 414         coord_t *c = &el->el_terminal.t_size;
 415 
 416         b = reallocarray(NULL, c->v + 1, sizeof(*b));
 417         if (b == NULL)
 418                 goto done;
 419         for (i = 0; i < c->v; i++) {
 420                 b[i] = reallocarray(NULL, c->h + 1, sizeof(**b));
 421                 if (b[i] == NULL) {
 422                         while (--i >= 0)
 423                                 free(b[i]);
 424                         free(b);
 425                         goto done;
 426                 }
 427         }
 428         b[c->v] = NULL;
 429         el->el_display = b;

The line buffers are el->el_display[] and they are allocated by
reallocarray().  Then they are initialized at re_clear_display().

1149 protected void
1150 re_clear_display(EditLine *el)
1151 {
1152         int i;
1153         
1154         el->el_cursor.v = 0;
1155         el->el_cursor.h = 0;
1156         for (i = 0; i < el->el_terminal.t_size.v; i++)
1157                 el->el_display[i][0] = '\0';
1158         el->el_refresh.r_oldcv = 0;
1159 }

Remark that only first char is set NUL, but the rest part is kept
uninitialized.  Then user inputs "deb".  re_fastputc() is called for
each char.

1053 re_fastputc(EditLine *el, wint_t c)
1054 {
1055         wchar_t *lastline;
1056         int w;
1057 
1058         w = wcwidth(c);
1059         while (w > 1 && el->el_cursor.h + w > el->el_terminal.t_size.h)
1060             re_fastputc(el, ' ');
1061 
1062         terminal__putc(el, c);
1063         el->el_display[el->el_cursor.v][el->el_cursor.h++] = c;

the function just put the char like #1063, then the buffer becomes it
doesn't include any NUL char like below.

  deb + <uninitialized>

After this, by calling re_reresh() the crash happens.

I had looked into the code deeply, but it didn't become clear how
should we fix this problem.  But I suppose the code assumes the entire
buffer is initialized, so the following diff replaces the code which
initializes only first char by the code which calls
re__copy_and_pad().

comment?
ok?

Index: lib/libedit/refresh.c
===================================================================
RCS file: /var/cvs/openbsd/src/lib/libedit/refresh.c,v
retrieving revision 1.22
diff -u -p -r1.22 refresh.c
--- lib/libedit/refresh.c       11 Oct 2018 15:19:09 -0000      1.22
+++ lib/libedit/refresh.c       1 Aug 2019 04:55:34 -0000
@@ -320,7 +320,8 @@ re_refresh(EditLine *el)
 #ifdef DEBUG_REFRESH
                        terminal_overwrite(el, L"C\b", 2);
 #endif /* DEBUG_REFRESH */
-                       el->el_display[i][0] = '\0';
+                       re__copy_and_pad(el->el_display[i], L"",
+                           (size_t) el->el_terminal.t_size.h);
                }
 
        el->el_refresh.r_oldcv = el->el_refresh.r_newcv; /* set for next time */
@@ -1154,7 +1155,8 @@ re_clear_display(EditLine *el)
        el->el_cursor.v = 0;
        el->el_cursor.h = 0;
        for (i = 0; i < el->el_terminal.t_size.v; i++)
-               el->el_display[i][0] = '\0';
+               re__copy_and_pad(el->el_display[i], L"",
+                   (size_t)el->el_terminal.t_size.h);
        el->el_refresh.r_oldcv = 0;
 }
 

Reply via email to