]> granicus.if.org Git - handbrake/commitdiff
LinGui: sanitize preset export filename
authorJohn Stebbins <jstebbins.hb@gmail.com>
Tue, 12 Feb 2019 14:22:45 +0000 (07:22 -0700)
committerJohn Stebbins <jstebbins.hb@gmail.com>
Tue, 12 Feb 2019 15:05:13 +0000 (08:05 -0700)
Removes illegal characters that may be in the preset name from the
suggested filename and trims leading and trailing white space.

gtk/src/ghbcompat.h
gtk/src/presets.c

index b4cf9deea4fecce6d0823b9f5408c9d1df414f0d..38212513298dc54e1eb9f2dc6d4379c529c477e2 100644 (file)
 #include <gdk/gdk.h>
 #include <string.h>
 
+#if defined(_WIN32)
+#define GHB_UNSAFE_FILENAME_CHARS "/:<>\"\\|?*"
+#else
+#define GHB_UNSAFE_FILENAME_CHARS "/"
+#endif
+
 static inline void ghb_widget_get_preferred_width(
     GtkWidget *widget, gint *min_width, gint * natural_width)
 {
index c88bb6b4fc985211cfea49a85ec10e2be3cf3316..9284b94f7a77a87bc46bf11fd193279796e59d15 100644 (file)
@@ -2026,17 +2026,18 @@ preset_export_action_cb(GSimpleAction *action, GVariant *param,
                         signal_user_data_t *ud)
 {
     hb_preset_index_t *path;
-    const gchar       *name;
     GtkWindow         *hb_window;
     GtkWidget         *dialog;
     GtkResponseType    response;
     const gchar       *exportDir;
     gchar             *filename;
     GhbValue          *dict;
+    char              *preset_name;
 
     path = get_selected_path(ud);
     if (path == NULL || path->depth <= 0)
     {
+        const gchar       *name;
         char * new_name;
 
         free(path);
@@ -2056,7 +2057,7 @@ preset_export_action_cb(GSimpleAction *action, GVariant *param,
     {
         return;
     }
-    name = ghb_dict_get_string(dict, "PresetName");
+    preset_name = g_strdup(ghb_dict_get_string(dict, "PresetName"));
 
     hb_window = GTK_WINDOW(GHB_WIDGET(ud->builder, "hb_window"));
     dialog = gtk_file_chooser_dialog_new(_("Export Preset"), hb_window,
@@ -2070,10 +2071,16 @@ preset_export_action_cb(GSimpleAction *action, GVariant *param,
     {
         exportDir = ".";
     }
-    filename = g_strdup_printf("%s.json", name);
+
+    // Clean up preset name for use as a filename.  Removing leading
+    // and trailing whitespace and filename illegal characters.
+    g_strstrip(preset_name);
+    g_strdelimit(preset_name, GHB_UNSAFE_FILENAME_CHARS, '_');
+    filename = g_strdup_printf("%s.json", preset_name);
     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), exportDir);
     gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), filename);
     g_free(filename);
+    g_free(preset_name);
 
     response = gtk_dialog_run(GTK_DIALOG(dialog));
     gtk_widget_hide(dialog);