Subject: bug#73022: 31.0.50; Crash in build_frame_matrix_from_leaf_window after C-x 2 and reducing terminal size

From: martin rudalics <[email-masked]>
Date: Sun, 8 Sep 2024 16:58:45 +0200

> I can cause the build_frame_matrix_from_leaf_window failed assertion (
 >    glyph_row_slice_p(window_row, frame_row) is false  ) when slowly
 > resizing row by row (7→6→5 rows). The number of columns doesn't matter
 > (can be a normal one).
 >
 > You can also try maximizing/unmaximizing“the window if you window
 > manager supports it. That's a way of suddenly changing the number of
 > rows from a normal value (e.g. 20) to a dangerous value (e.g. 4).
 > After unmaximizing it immediately crashes (if you did the C-x 2
 > split).

Please try the attached diff (from my heavily edited copy of master, if
it doesn't apply cleanly, complain immediately rather than messing up
your Emacs).  It should fix two silly bugs in window.el that make a
frame's safe minimum size much too large and includes the fix I proposed
earlier here.

And it moves the assignment of FrameRows to handle_window_change_signal
in dispnew.c.  Doing it in adjust_frame_size was silly (as Gerd Möllmann
noticed earlier).  FrameRows should be the height of the tty which can
be smaller than the height of our frame.  Emacs is supposed to store it
but not to modify it according to our capabilities.

The patch does not fix the cmcheckmagic problem but makes it much less
likely to occur.  And redisplay of an insanely small tty window looks
much better here.

martin
diff --git a/lisp/window.el b/lisp/window.el
index f4226fa4428..e648ccb270a 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -1692,9 +1692,10 @@ window--min-size-1
 		     0
 		   (window-min-pixel-height window)))
 	      (max (ceiling pixel-height char-size)
-		   (if (window--min-size-ignore-p window ignore)
-		       0
-		     window-min-height))))))))))
+		   (cond
+		    ((window--min-size-ignore-p window ignore) 0)
+		    ((window-minibuffer-p window) 1)
+		    (t window-min-height)))))))))))
 
 (defun window-sizable (window delta &optional horizontal ignore pixelwise)
   "Return DELTA if DELTA lines can be added to WINDOW.
@@ -1888,7 +1889,7 @@ frame-windows-min-size
 	 (mini (window-next-sibling root)))
     (+ (window-min-size root horizontal ignore pixelwise)
        (if (and mini (not horizontal))
-	   (window-min-size mini horizontal nil pixelwise)
+	   (window-min-size mini horizontal ignore pixelwise)
 	 0))))
 
 (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
diff --git a/src/dispnew.c b/src/dispnew.c
index 1a243079e46..8a57866d3ed 100644
--- a/src/dispnew.c
+++ b/src/dispnew.c
@@ -6054,6 +6054,11 @@ change_frame_size_1 (struct frame *f, int new_width, int new_height,
       adjust_frame_size (f, FRAME_PIXEL_TO_TEXT_WIDTH (f, new_width),
 			 FRAME_PIXEL_TO_TEXT_HEIGHT (f, new_height), 5,
 			 pretend, Qchange_frame_size);
+      if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
+	{
+	  FrameRows (FRAME_TTY (f)) = new_height;
+	  FrameCols (FRAME_TTY (f)) = new_width;
+	}
     }
 }
 
diff --git a/src/frame.c b/src/frame.c
index 7f4bf274ad9..9908ec307f7 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -827,14 +827,26 @@ adjust_frame_size (struct frame *f, int new_text_width, int new_text_height,
   new_text_lines = dos_new_text_lines - FRAME_TOP_MARGIN (f);
 #endif
 
+  /* Assign new sizes.  Do it here to make sure that frame based
+     redisplay gets congruent sizes for the dimensions of the frame
+     matrix and the combined window matrices.  */
+  FRAME_COLS (f) = new_text_cols;
+  FRAME_LINES (f) = new_text_lines;
+  FRAME_TEXT_WIDTH (f) = new_text_width;
+  FRAME_TEXT_HEIGHT (f) = new_text_height;
+  FRAME_PIXEL_WIDTH (f) = new_native_width;
+  FRAME_PIXEL_HEIGHT (f) = new_native_height;
+  FRAME_TOTAL_COLS (f) = FRAME_PIXEL_WIDTH (f) / FRAME_COLUMN_WIDTH (f);
+  FRAME_TOTAL_LINES (f) = FRAME_PIXEL_HEIGHT (f) / FRAME_LINE_HEIGHT (f);
+
   if (new_inner_width != old_inner_width)
     {
       resize_frame_windows (f, new_inner_width, true);
 
       /* MSDOS frames cannot PRETEND, as they change frame size by
 	 manipulating video hardware.  */
-      if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
-	FrameCols (FRAME_TTY (f)) = new_text_cols;
+/**       if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f)) **/
+/** 	FrameCols (FRAME_TTY (f)) = new_text_cols; **/
 
 #if defined (HAVE_WINDOW_SYSTEM)
       if (WINDOWP (f->tab_bar_window))
@@ -867,22 +879,12 @@ adjust_frame_size (struct frame *f, int new_text_width, int new_text_height,
 
       /* MSDOS frames cannot PRETEND, as they change frame size by
 	 manipulating video hardware.  */
-      if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
-	FrameRows (FRAME_TTY (f)) = new_text_lines + FRAME_TOP_MARGIN (f);
+/**       if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f)) **/
+/** 	FrameRows (FRAME_TTY (f)) = new_text_lines + FRAME_TOP_MARGIN (f); **/
     }
   else if (new_text_lines != old_text_lines)
     call2 (Qwindow__pixel_to_total, frame, Qnil);
 
-  /* Assign new sizes.  */
-  FRAME_COLS (f) = new_text_cols;
-  FRAME_LINES (f) = new_text_lines;
-  FRAME_TEXT_WIDTH (f) = new_text_width;
-  FRAME_TEXT_HEIGHT (f) = new_text_height;
-  FRAME_PIXEL_WIDTH (f) = new_native_width;
-  FRAME_PIXEL_HEIGHT (f) = new_native_height;
-  FRAME_TOTAL_COLS (f) = FRAME_PIXEL_WIDTH (f) / FRAME_COLUMN_WIDTH (f);
-  FRAME_TOTAL_LINES (f) = FRAME_PIXEL_HEIGHT (f) / FRAME_LINE_HEIGHT (f);
-
   {
     struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
     int text_area_x, text_area_y, text_area_width, text_area_height;