From 6aaefa73c2dd20df971e7c02c5e1df71ffa02b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20M=C3=ADchal?= Date: Sat, 23 Aug 2025 21:39:32 +0300 Subject: [PATCH] libgimp: Sanitize path separators in menu paths Technically not problematic but when inspecting using Plugin Browser, the resulting hierarchy is confusing because it sometimes shows actions to be inside "empty" directories if there are doubled path separators or trailing ones. Let's not depend on the user to do this correctly when we can do it for them. --- libgimp/gimpprocedure.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/libgimp/gimpprocedure.c b/libgimp/gimpprocedure.c index a3a2f64d5b..05b2de0b5c 100644 --- a/libgimp/gimpprocedure.c +++ b/libgimp/gimpprocedure.c @@ -599,6 +599,28 @@ gimp_procedure_create_config_with_prefix (GimpProcedure *procedure, NULL); } +static gchar * +sanitize_menu_path_separators (const gchar *menu_path) +{ + gchar *menu_path_clean = g_strdup (menu_path); + gchar *hit = NULL; + + /* Get rid of all trailing path separators */ + while (g_str_has_suffix (menu_path_clean, "/")) + menu_path_clean[strlen (menu_path_clean) - 1] = '\0'; + + /* Search the menu path for all occurences of doubled path separators. */ + while ((hit = strstr (menu_path_clean, "//")) != 0) + { + /* Shift the string by one to overwrite the first path separator while + * preserving the end of the string without having to re-allocate. */ + for (; *hit != '\0'; hit++) + *hit = *(hit+1); + } + + return menu_path_clean; +} + /* public functions */ @@ -962,7 +984,8 @@ void gimp_procedure_add_menu_path (GimpProcedure *procedure, const gchar *menu_path) { - GimpProcedurePrivate *priv; + GimpProcedurePrivate *priv = NULL; + gchar *menu_path_clean = NULL; g_return_if_fail (GIMP_IS_PROCEDURE (procedure)); g_return_if_fail (menu_path != NULL); @@ -971,11 +994,14 @@ gimp_procedure_add_menu_path (GimpProcedure *procedure, g_return_if_fail (priv->menu_label != NULL); - priv->menu_paths = g_list_append (priv->menu_paths, g_strdup (menu_path)); + /* sanitize_menu_path_separators already creates a copy of menu_path */ + menu_path_clean = sanitize_menu_path_separators (menu_path); + + priv->menu_paths = g_list_append (priv->menu_paths, menu_path_clean); if (priv->installed) _gimp_pdb_add_proc_menu_path (gimp_procedure_get_name (procedure), - menu_path); + menu_path_clean); } /**