Subject: Re: bug#41627: 28.0.50; Emacs with cairo build segfault in HELLO file

From: Pip Cet <[email-masked]>
Date: Sun, 31 May 2020 20:45:56 +0000

On Sun, May 31, 2020 at 5:24 PM Eli Zaretskii <[email-masked]> wrote:
> Can someone please propose a patch along these lines?  I cannot easily
> test a Cairo build, so I won't try showing a patch.

How about this?

(I'm not sure how and whether `match' is supposed to be freed in the
success case, or whether it's simply leaked).
From dfee83880893258ba0d3c8bf8ba4737d5f99b586 Mon Sep 17 00:00:00 2001
From: Pip Cet <[email-masked]>
Date: Sun, 31 May 2020 20:44:57 +0000
Subject: [PATCH] Handle Cairo errors in ftcrfont_open (bug#41627)

* src/ftcrfont.c (ftcrfont_open): Handle Cairo errors.
---
 src/ftcrfont.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/ftcrfont.c b/src/ftcrfont.c
index 7832d4f5ce..88445c2a90 100644
--- a/src/ftcrfont.c
+++ b/src/ftcrfont.c
@@ -139,7 +139,8 @@ ftcrfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
 
   FcPatternDestroy (pat);
   font_face = cairo_ft_font_face_create_for_pattern (match);
-  if (!font_face)
+  if (!font_face
+      || cairo_font_face_status (font_face) != CAIRO_STATUS_SUCCESS)
     {
       unblock_input ();
       FcPatternDestroy (match);
@@ -154,6 +155,18 @@ ftcrfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
   cairo_font_face_destroy (font_face);
   cairo_font_options_destroy (options);
   unblock_input ();
+  if (!scaled_font
+      || cairo_scaled_font_status (scaled_font) != CAIRO_STATUS_SUCCESS)
+    {
+      FcPatternDestroy (match);
+      return Qnil;
+    }
+  ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
+  if (!ft_face)
+    {
+      FcPatternDestroy (match);
+      return Qnil;
+    }
 
   font_object = font_build_object (VECSIZE (struct font_info),
 				   AREF (entity, FONT_TYPE_INDEX),
@@ -231,7 +244,6 @@ ftcrfont_open (struct frame *f, Lisp_Object entity, int pixel_size)
       font->descent = font->height - font->ascent;
     }
 
-  ft_face = cairo_ft_scaled_font_lock_face (scaled_font);
   if (XFIXNUM (AREF (entity, FONT_SIZE_INDEX)) == 0)
     {
       int upEM = ft_face->units_per_EM;
-- 
2.27.0.rc0