From 9d43b2cfc2da2bda910010696616758aaaddb946 Mon Sep 17 00:00:00 2001 From: Idriss Fekir Date: Sat, 3 May 2025 01:29:47 +0200 Subject: [PATCH] GimpText: Fix crash when serializing text with missing font. For some reason pango returns a FontDescription with name "Normal" when called with empty string. This caused crashes in pango markup serialization. This patch checks if the font before serialization is a valid font object, otherwise fallback to the fallback font. Also font's reference count wasn't increased in one case when opening xcf file with a text layer. --- app/text/gimpfont.c | 7 ++++++- app/text/gimptext.c | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/text/gimpfont.c b/app/text/gimpfont.c index b8559bfe47..2e509c0554 100644 --- a/app/text/gimpfont.c +++ b/app/text/gimpfont.c @@ -333,7 +333,12 @@ gimp_font_deserialize_create (GType type, } if (g_scanner_peek_next_token (scanner) == G_TOKEN_RIGHT_PAREN) - return GIMP_CONFIG (GIMP_FONT (gimp_font_get_standard ())); + { + font = GIMP_FONT (gimp_font_get_standard ()); + g_object_ref (font); + + return GIMP_CONFIG (font); + } scope_id = g_type_qname (type); old_scope_id = g_scanner_set_scope (scanner, scope_id); diff --git a/app/text/gimptext.c b/app/text/gimptext.c index 2ee46633da..f8eb750a06 100644 --- a/app/text/gimptext.c +++ b/app/text/gimptext.c @@ -587,6 +587,9 @@ gimp_text_set_property (GObject *object, if (font != text->font && font != NULL) g_set_object (&text->font, font); + /* this is defensive to avoid some crashes */ + else if (font == NULL) + g_set_object (&text->font, GIMP_FONT (gimp_font_get_standard ())); } break; case PROP_FONT_SIZE: @@ -844,6 +847,12 @@ gimp_text_serialize_property (GimpConfig *config, font = GIMP_FONT (gimp_container_search (container, (GimpContainerSearchFunc) gimp_font_match_by_lookup_name, (gpointer) altered_font_name)); + /* in case pango returns a non existant font name */ + if (font == NULL) + { + font = GIMP_FONT (gimp_font_get_standard ()); + font_name = "gimpfont"; + } gimp_config_writer_open (writer, "markupfont"); /*lookupname format is "gimpfont%d" we keep only the "font%d", @@ -955,6 +964,7 @@ gimp_text_deserialize_property (GimpConfig *object, scanner, -1, NULL)); + g_assert (GIMP_IS_FONT (font)); replaced_markup = g_markup_printf_escaped (" font=\"%s\"", markup_fontname); new_markup = g_strdup_printf (" gimpfont=\"%s\"", gimp_font_get_lookup_name (font)); g_string_replace (markup_str, replaced_markup, new_markup, 0); @@ -993,6 +1003,7 @@ gimp_text_deserialize_property (GimpConfig *object, scanner, -1, NULL)); + g_assert (GIMP_IS_FONT (font)); g_scanner_get_next_token (scanner); /* ) */ g_scanner_get_next_token (scanner); /* ) */