Merge branch 'wip/chergert/avoid-malloc-in-format-string-validation' into 'main'

gvariant: Avoid malloc/free in valid_format_string()

See merge request GNOME/glib!4295
This commit is contained in:
Philip Withnall 2024-09-25 13:10:34 +00:00
commit e071e1bfc0

View File

@ -4671,6 +4671,15 @@ valid_format_string (const gchar *format_string,
const gchar *endptr; const gchar *endptr;
GVariantType *type; GVariantType *type;
/* An extremely common use-case is checking the format string without
* caring about the value specifically. Provide a fast-path for this to
* avoid the malloc/free overhead.
*/
if G_LIKELY (value == NULL &&
g_variant_format_string_scan (format_string, NULL, &endptr) &&
(single || *endptr == '\0'))
return TRUE;
type = g_variant_format_string_scan_type (format_string, NULL, &endptr); type = g_variant_format_string_scan_type (format_string, NULL, &endptr);
if G_UNLIKELY (type == NULL || (single && *endptr != '\0')) if G_UNLIKELY (type == NULL || (single && *endptr != '\0'))