Subject: bug#28346: 24.5; Emacs hangs when terminal becomes smaller than set margin

From: yorwba <[email-masked]>
Date: Mon, 4 Sep 2017 17:49:25 +0800

This bug only appears when Emacs is run in a terminal emulator that
allows changing the window size.

Steps to reproduce:

1. Run `emacs -nw -Q` in a small terminal window
2. Evaluate (setq width (window-width (get-buffer-window (current-buffer))))
   The value is 74 in my case.
3. Enlarge the terminal window (I go fullscreen with 150 columns).
4. Evaluate (set-window-margins (get-buffer-window (current-buffer)) 0 width)
5. Resize the terminal emulator window back to the original size (or smaller).
6. The terminal goes blank and no longer reacts to input.

Attaching gdb and using finish to end all terminating functions, I found that
it gets stuck in move_it_vertically_backwards (in xdisp.c). It keeps taking
the goto move_further_back without actually moving further back.

I believe that this is caused by incorrectly adjusting the margins when the
total available width changes.

What I see in GDB:

(gdb) p *it->w
$2 = {header = {size = 4611686018494636052}, frame = 12472813, next = 12473765, prev = 12392562, parent = 12392562, normal_lines = 12367919, 
  normal_cols = 12367927, new_total = 0, new_normal = 0, new_pixel = 0, contents = 12419381, start = 12418371, pointm = 12418411, 
  temslot = 12392562, vertical_scroll_bar = 12392562, vertical_scroll_bar_type = 12392610, display_table = 12392562, dedicated = 12392562, 
  redisplay_end_trigger = 12392562, combination_limit = 12392562, window_parameters = 12392562, current_matrix = 0xbe8ce0, 
  desired_matrix = 0xbe8c70, prev_buffers = 12392562, next_buffers = 12392562, use_time = 1, sequence_number = 1, pixel_left = 0, pixel_top = 1, 
  left_col = 0, top_line = 1, pixel_width = 74, pixel_height = 40, total_cols = 74, total_lines = 40, hscroll = 0, min_hscroll = 0, 
  last_modified = 0, last_overlay_modified = 0, last_point = 322, base_line_number = 0, base_line_pos = 1, column_number_displayed = -1, 
  nrows_scale_factor = 1, ncols_scale_factor = 1, cursor = {x = 0, y = 6, hpos = 0, vpos = -1}, phys_cursor = {x = 0, y = 0, hpos = 0, vpos = 0}, 
  output_cursor = {x = 0, y = 0, hpos = 0, vpos = 0}, last_cursor_vpos = 6, phys_cursor_type = NO_CURSOR, phys_cursor_width = -1, 
  phys_cursor_ascent = 0, phys_cursor_height = 0, left_fringe_width = -1, right_fringe_width = -1, left_margin_cols = 0, right_margin_cols = 74, 
  scroll_bar_width = -1, mode_line_height = 1, header_line_height = -1, window_end_pos = 282, window_end_vpos = 38, mini = false, 
  horizontal = false, update_mode_line = true, last_had_star = true, start_at_line_beg = true, force_start = false, optional_new_start = false, 
  phys_cursor_on_p = false, cursor_off_p = false, last_cursor_off_p = false, must_be_updated_p = true, pseudo_window_p = false, 
  fringes_outside_margins = false, window_end_valid = false, redisplay = true, vscroll = 0, window_end_bytepos = 282}
(gdb) p *it->w->current_matrix
$3 = {pool = 0xbaaca0, rows = 0xc757b0, rows_allocated = 60, nrows = 40, matrix_x = 0, matrix_y = 1, matrix_w = 74, matrix_h = 40, 
  window_pixel_left = 0, window_pixel_top = 1, window_height = 39, window_width = 74, window_vscroll = 0, left_margin_glyphs = 0, 
  right_margin_glyphs = 36, no_scrolling_p = false, header_line_p = false, buffer = 0xbd8130, begv = 1, zv = 322}
(gdb) p *it->w->desired_matrix
$4 = {pool = 0xbaabd0, rows = 0xc64800, rows_allocated = 60, nrows = 40, matrix_x = 0, matrix_y = 1, matrix_w = 74, matrix_h = 40, 
  window_pixel_left = 0, window_pixel_top = 1, window_height = 39, window_width = 74, window_vscroll = 0, left_margin_glyphs = 0, 
  right_margin_glyphs = 36, no_scrolling_p = false, header_line_p = false, buffer = 0x0, begv = 0, zv = 0}

Notice that current_matrix and desired_matrix both have window_width = 74 and right_margin_glyphs = 36,
but the window itself has total_cols = 74 and right_margin_cols = 74.

I hope that's enough info to reproduce and fix this issue.

In GNU Emacs 24.5.1 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9)
 of 2016-04-17 on lgw01-04, modified by Debian
System Description:	Ubuntu 16.04.3 LTS

Configured using:
 `configure --build x86_64-linux-gnu --prefix=/usr
 --sharedstatedir=/var/lib --libexecdir=/usr/lib
 --localstatedir=/var/lib --infodir=/usr/share/info
 --mandir=/usr/share/man --with-pop=yes
 --enable-locallisppath=/etc/emacs24:/etc/emacs:/usr/local/share/emacs/24.5/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/24.5/site-lisp:/usr/share/emacs/site-lisp
 --build x86_64-linux-gnu --prefix=/usr --sharedstatedir=/var/lib
 --libexecdir=/usr/lib --localstatedir=/var/lib
 --infodir=/usr/share/info --mandir=/usr/share/man --with-pop=yes
 --enable-locallisppath=/etc/emacs24:/etc/emacs:/usr/local/share/emacs/24.5/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/24.5/site-lisp:/usr/share/emacs/site-lisp
 --with-x=yes --with-x-toolkit=gtk3 --with-toolkit-scroll-bars
 'CFLAGS=-g -O2 -fstack-protector-strong -Wformat
 -Werror=format-security -Wall' 'CPPFLAGS=-Wdate-time
 -D_FORTIFY_SOURCE=2' 'LDFLAGS=-Wl,-Bsymbolic-functions -Wl,-z,relro''

Important settings:
  value of $LC_MONETARY: en_US.UTF-8
  value of $LC_NUMERIC: en_US.UTF-8
  value of $LC_TIME: en_US.UTF-8
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=fcitx
  locale-coding-system: utf-8-unix

Major mode: Fundamental

Minor modes in effect:
  tooltip-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  line-number-mode: t
  transient-mark-mode: t

Recent messages:
Loading debian-ispell...done
Loading /var/cache/dictionaries-common/emacsen-ispell-dicts.el (source)...done
Loading /etc/emacs/site-start.d/50dictionaries-common.el (source)...done
Loading /etc/emacs/site-start.d/50latex-cjk-common.el (source)...
Loading cjk-enc...done
Loading /etc/emacs/site-start.d/50latex-cjk-common.el (source)...done
Loading /etc/emacs/site-start.d/50latex-cjk-thai.el (source)...done
Loading /etc/emacs/site-start.d/50texlive-lang-english.el (source)...done
For information about GNU Emacs and the GNU system, type C-h C-a.
Making completion list...

Load-path shadows:
/usr/share/emacs/24.5/site-lisp/debian-startup hides /usr/share/emacs/site-lisp/debian-startup
/usr/share/emacs24/site-lisp/cmake-data/cmake-mode hides /usr/share/emacs/site-lisp/cmake-mode
/usr/share/emacs24/site-lisp/latex-cjk-thai/thai-word hides /usr/share/emacs/24.5/lisp/language/thai-word

Features:
(shadow sort gnus-util mail-extr emacsbug message format-spec rfc822 mml
mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums mm-util
help-fns mail-prsvr mail-utils help-mode easymenu xterm time-date
tooltip electric uniquify ediff-hook vc-hooks lisp-float-type mwheel
x-win x-dnd tool-bar dnd fontset image regexp-opt fringe tabulated-list
newcomment lisp-mode prog-mode register page menu-bar rfn-eshadow timer
select scroll-bar mouse jit-lock font-lock syntax facemenu font-core
frame cham georgian utf-8-lang misc-lang vietnamese tibetan thai
tai-viet lao korean japanese hebrew greek romanian slovak czech european
ethiopic indian cyrillic chinese case-table epa-hook jka-cmpr-hook help
simple abbrev minibuffer nadvice loaddefs button faces cus-face macroexp
files text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote make-network-process
dbusbind gfilenotify dynamic-setting system-font-setting
font-render-setting move-toolbar gtk x-toolkit x multi-tty emacs)

Memory information:
((conses 16 79653 6239)
 (symbols 48 17875 0)
 (miscs 40 71 113)
 (strings 32 10754 4684)
 (string-bytes 1 267933)
 (vectors 16 7270)
 (vector-slots 8 343931 30089)
 (floats 8 66 517)
 (intervals 56 212 0)
 (buffers 960 13)
 (heap 1024 12619 1914))