]> git.eshelyaron.com Git - emacs.git/commitdiff
Make ftcrfont_close idempotent
authorPip Cet <pipcet@protonmail.com>
Sat, 10 Aug 2024 16:57:49 +0000 (16:57 +0000)
committerEshel Yaron <me@eshelyaron.com>
Sun, 11 Aug 2024 07:31:53 +0000 (09:31 +0200)
This prevents crashes when running code like:

(let ((font (open-font (find-font (font-spec ...)))))
  (close-font font)
  (close-font font))

* src/ftcrfont.c (ftcrfont_close): Make sure the function can be run
several times in a row without crashing.

(cherry picked from commit 1a38936299d3c93767e4b542ccde990ea08379e3)

src/ftcrfont.c

index 5ee375c782b8ee84d38daff404738d6e8ae1cbcc..3700154e44ad9699b31a6343aaa55ba363a74bb6 100644 (file)
@@ -344,12 +344,20 @@ ftcrfont_close (struct font *font)
       ftcrfont_info->hb_font = NULL;
     }
 #endif
-  for (int i = 0; i < ftcrfont_info->metrics_nrows; i++)
-    if (ftcrfont_info->metrics[i])
-      xfree (ftcrfont_info->metrics[i]);
   if (ftcrfont_info->metrics)
-    xfree (ftcrfont_info->metrics);
-  cairo_scaled_font_destroy (ftcrfont_info->cr_scaled_font);
+    {
+      for (int i = 0; i < ftcrfont_info->metrics_nrows; i++)
+       if (ftcrfont_info->metrics[i])
+         xfree (ftcrfont_info->metrics[i]);
+      if (ftcrfont_info->metrics)
+       xfree (ftcrfont_info->metrics);
+      ftcrfont_info->metrics = NULL;
+    }
+  if (ftcrfont_info->cr_scaled_font)
+    {
+      cairo_scaled_font_destroy (ftcrfont_info->cr_scaled_font);
+      ftcrfont_info->cr_scaled_font = NULL;
+    }
   unblock_input ();
 }