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.
This commit is contained in:
Idriss Fekir 2025-05-03 01:29:47 +02:00
parent 12806b2830
commit 9d43b2cfc2
2 changed files with 17 additions and 1 deletions

View file

@ -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);

View file

@ -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); /* ) */