From bfa39299e88f2892a15ffabee731f7cd5c044603 Mon Sep 17 00:00:00 2001
From: Naofumi Yasufuku <naofumi@yasufuku.dev>
Date: Sun, 30 May 2021 16:33:34 +0900
Subject: [PATCH] free_realized_face printf logs for lisp_string_width segfault
 under macOS

Bug#48732 28.0.50; lisp_string_width segfaults on startup under macOS
---
 src/alloc.c   |  1 +
 src/font.c    |  4 ++++
 src/fontset.c |  3 +++
 src/xfaces.c  | 34 +++++++++++++++++++++++++++++++---
 4 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/alloc.c b/src/alloc.c
index 76d8c7ddd1..3846c4bc18 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -822,6 +822,7 @@ xfree (void *block)
     return;
   MALLOC_BLOCK_INPUT;
   free (block);
+  fprintf (stderr, "%s: block=%p\n", __func__, block);
   MALLOC_UNBLOCK_INPUT;
   /* We don't call refill_memory_reserve here
      because in practice the call in r_alloc_free seems to suffice.  */
diff --git a/src/font.c b/src/font.c
index 7c1d1ff89b..b49a4536d0 100644
--- a/src/font.c
+++ b/src/font.c
@@ -3126,6 +3126,9 @@ font_clear_prop (Lisp_Object *attrs, enum font_property_index prop)
 font_select_entity (struct frame *f, Lisp_Object entities,
 		    Lisp_Object *attrs, int pixel_size, int c)
 {
+  fprintf (stderr, "%s: frame f=%p: attrs=%p\n",
+	   __func__, f, attrs);
+
   Lisp_Object font_entity;
   Lisp_Object prefer;
   int i;
@@ -3866,6 +3869,7 @@ font_range (ptrdiff_t pos, ptrdiff_t pos_byte, ptrdiff_t *limit,
 	                                     face_id, false, 0);
 	}
       face = FACE_FROM_ID (f, face_id);
+      fprintf (stderr, "%s: frame f=%p: face_id=%d: face=%p\n", __func__, f, face_id, face);
     }
 
   while (pos < *limit)
diff --git a/src/fontset.c b/src/fontset.c
index 332be6c39d..213c2b6786 100644
--- a/src/fontset.c
+++ b/src/fontset.c
@@ -557,6 +557,9 @@ fontset_find_font (Lisp_Object fontset, int c, struct face *face,
 		     : XFRAME (selected_frame));
   Lisp_Object rfont_def;
 
+  fprintf (stderr, "%s: frame f=%p: XFRAME(FONTSET_FRAME(fontset)=%p: XFRAME(selected_frame)=%p: face=%p\n",
+	   __func__, f, XFRAME (FONTSET_FRAME (fontset)), XFRAME (selected_frame), face);
+
   font_group = fontset_get_font_group (fontset, fallback ? -1 : c);
   if (! CONSP (font_group))
     return font_group;
diff --git a/src/xfaces.c b/src/xfaces.c
index ab4440f46a..a3b74926e3 100644
--- a/src/xfaces.c
+++ b/src/xfaces.c
@@ -4407,6 +4407,8 @@ make_realized_face (Lisp_Object *attr)
 static void
 free_realized_face (struct frame *f, struct face *face)
 {
+  fprintf (stderr, "%s: frame f=%p: face=%p\n",
+	   __func__, f, face);
   if (face)
     {
 #ifdef HAVE_WINDOW_SYSTEM
@@ -4613,6 +4615,8 @@ free_realized_faces (struct face_cache *c)
 
       for (i = 0; i < c->used; ++i)
 	{
+	  fprintf (stderr, "%s: frame f=%p: c->faces_by_id[i]\n",
+		   __func__, f, c->faces_by_id[i]);
 	  free_realized_face (f, c->faces_by_id[i]);
 	  c->faces_by_id[i] = NULL;
 	}
@@ -4647,13 +4651,18 @@ free_all_realized_faces (Lisp_Object frame)
 {
   if (NILP (frame))
     {
+      fprintf (stderr, "%s: NILP (frame)\n",
+	       __func__);
       Lisp_Object rest;
       FOR_EACH_FRAME (rest, frame)
 	free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
       windows_or_buffers_changed = 58;
     }
-  else
+  else {
+    fprintf (stderr, "%s: ! NILP (frame)\n",
+	     __func__);
     free_realized_faces (FRAME_FACE_CACHE (XFRAME (frame)));
+  }
 }
 
 
@@ -4664,6 +4673,8 @@ free_face_cache (struct face_cache *c)
 {
   if (c)
     {
+      fprintf (stderr, "%s: frame c->f=%p\n",
+	       __func__, c->f);
       free_realized_faces (c);
       xfree (c->buckets);
       xfree (c->faces_by_id);
@@ -4765,6 +4776,9 @@ uncache_face (struct face_cache *c, struct face *face)
   c->faces_by_id[face->id] = NULL;
   if (face->id == c->used)
     --c->used;
+
+  fprintf (stderr, "%s: face->id=%d: c->faces_by_id[face->id]=%p: c->used=%d\n",
+	   __func__, face->id, c->faces_by_id[face->id], c->used);
 }
 
 
@@ -5822,18 +5836,28 @@ realize_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE],
       /* Remove the former face.  */
       struct face *former_face = cache->faces_by_id[former_face_id];
       uncache_face (cache, former_face);
+      fprintf (stderr, "%s: frame cache->f=%p: former_face_id=%d: former_face=%p\n",
+	       __func__, cache->f, former_face_id, former_face);
       free_realized_face (cache->f, former_face);
       SET_FRAME_GARBAGED (cache->f);
     }
 
-  if (FRAME_WINDOW_P (cache->f))
+  if (FRAME_WINDOW_P (cache->f)) {
     face = realize_gui_face (cache, attrs);
-  else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f))
+    fprintf (stderr, "%s: realize_gui_face: face=%p: face->lface=%p\n",
+	     __func__, face, face->lface);
+  }
+  else if (FRAME_TERMCAP_P (cache->f) || FRAME_MSDOS_P (cache->f)) {
     face = realize_tty_face (cache, attrs);
+    fprintf (stderr, "%s: realize_tty_face: face=%p: face->lface=%p\n",
+	     __func__, face, face->lface);
+  }
   else if (FRAME_INITIAL_P (cache->f))
     {
       /* Create a dummy face. */
       face = make_realized_face (attrs);
+      fprintf (stderr, "%s: make_realized_face: face=%p: face->lface=%p\n",
+	       __func__, face, face->lface);
     }
   else
     emacs_abort ();
@@ -5897,6 +5921,8 @@ realize_gui_face (struct face_cache *cache, Lisp_Object attrs[LFACE_VECTOR_SIZE]
 
   /* Allocate a new realized face.  */
   face = make_realized_face (attrs);
+  fprintf (stderr, "%s: make_realized_face: face=%p: face->lface=%p\n",
+	   __func__, face, face->lface);
   face->ascii_face = face;
 
   f = cache->f;
@@ -6245,6 +6271,8 @@ realize_tty_face (struct face_cache *cache,
 
   /* Allocate a new realized face.  */
   face = make_realized_face (attrs);
+  fprintf (stderr, "%s: make_realized_face: face=%p: face->lface=%p\n",
+	   __func__, face, face->lface);
 #if false
   face->font_name = FRAME_MSDOS_P (cache->f) ? "ms-dos" : "tty";
 #endif
-- 
2.31.1

