[gimp] Fix 8452, crashing scripts.



commit 031601a92a64993e2f89ba518b13c7e9603ab673
Author: lloyd konneker <konnekerl gmail com>
Date:   Wed Aug 3 15:18:09 2022 -0400

    Fix 8452, crashing scripts.
    
    Bugs in earlier enhancements re GFile, GEnum.

 plug-ins/script-fu/libscriptfu/scheme-wrapper.c | 15 +++++--
 plug-ins/script-fu/libscriptfu/script-fu-arg.c  | 57 ++++++++++++++++++-------
 2 files changed, 54 insertions(+), 18 deletions(-)
---
diff --git a/plug-ins/script-fu/libscriptfu/scheme-wrapper.c b/plug-ins/script-fu/libscriptfu/scheme-wrapper.c
index c3f0256ff0..92627fe1e1 100644
--- a/plug-ins/script-fu/libscriptfu/scheme-wrapper.c
+++ b/plug-ins/script-fu/libscriptfu/scheme-wrapper.c
@@ -902,10 +902,19 @@ script_fu_marshal_procedure_call (scheme   *sc,
             return script_type_error (sc, "numeric", i, proc_name);
           else
             {
-              GimpItem *item =
-                gimp_item_get_by_id (sc->vptr->ivalue (sc->vptr->pair_car (a)));
+              gint item_ID;
+              item_ID = sc->vptr->ivalue (sc->vptr->pair_car (a));
 
-              g_value_set_object (&value, item);
+              /* Avoid failed assertion in libgimp.*/
+              if (gimp_item_id_is_valid (item_ID))
+                {
+                  GimpItem *item = gimp_item_get_by_id (item_ID);
+                  g_value_set_object (&value, item);
+                }
+              else
+                {
+                  return script_error (sc, "runtime: invalid item ID", a);
+                }
             }
         }
       else if (GIMP_VALUE_HOLDS_INT32_ARRAY (&value))
diff --git a/plug-ins/script-fu/libscriptfu/script-fu-arg.c b/plug-ins/script-fu/libscriptfu/script-fu-arg.c
index 9d11d2bf20..c7f7b3b62b 100644
--- a/plug-ins/script-fu/libscriptfu/script-fu-arg.c
+++ b/plug-ins/script-fu/libscriptfu/script-fu-arg.c
@@ -505,27 +505,39 @@ script_fu_arg_append_repr_from_gvalue (SFArg       *arg,
     case SF_FILENAME:
     case SF_DIRNAME:
       {
-        gchar * filepath = "error in file arg";
-
-        /* sanity: GValue initialized. */
-        if (G_VALUE_HOLDS_GTYPE(gvalue))
+        if (G_VALUE_HOLDS_OBJECT (gvalue) && G_VALUE_TYPE (gvalue) == G_TYPE_FILE)
           {
-            if (g_value_get_gtype (gvalue) == G_TYPE_FILE)
+            GFile *file = g_value_get_object (gvalue);
+
+            /* Catch: GValue initialized to hold a GFile, but not hold one.
+             * Specificially, GimpProcedureDialog can yield that condition;
+             * the dialog shows "(None)" meaning user has not chosen a file yet.
+             */
+            if (G_IS_FILE (file))
               {
-                filepath = g_file_get_path (g_value_get_object (gvalue));
+                /* Not checking file exists, only creating a descriptive string.
+                 * I.E. not g_file_get_path, which can return NULL.
+                 */
+                gchar *filepath = g_file_get_parse_name (file);
+                /* assert filepath not null. */
                 /* Not escape special chars for whitespace or double quote. */
+                g_string_append_printf (result_string, "\"%s\"", filepath);
+                g_free (filepath);
               }
             else
               {
-                g_warning ("Expecting GFile in gvalue.");
+                gchar *msg = "Invalid GFile in gvalue.";
+                g_warning ("%s", msg);
+                g_string_append_printf (result_string, "\"%s\"", msg);
               }
           }
         else
           {
-            g_warning ("Expecting GValue holding a GType");
+            gchar *msg = "Expecting GFile in gvalue.";
+            g_warning ("%s", msg);
+            g_string_append_printf (result_string, "\"%s\"", msg);
           }
-
-        g_string_append_printf (result_string, "\"%s\"", filepath);
+        /* Ensure appended a filepath string OR an error string.*/
       }
       break;
 
@@ -547,10 +559,6 @@ script_fu_arg_append_repr_from_gvalue (SFArg       *arg,
       break;
 
     case SF_OPTION:
-    case SF_ENUM:
-      /* When sanity test fails, return an arbitrary int.
-       * Fails when GimpConfig or GimpProcedureDialog does not support GParamEnum.
-       */
       if (G_VALUE_HOLDS_INT (gvalue))
         {
           g_string_append_printf (result_string, "%d", g_value_get_int (gvalue));
@@ -558,9 +566,28 @@ script_fu_arg_append_repr_from_gvalue (SFArg       *arg,
       else
         {
           g_warning ("Expecting GValue holding an int.");
-          g_string_append (result_string, "1");   /* Arbitrarily a common int. */
+          /* Append arbitrary int, so no errors in signature of Scheme call.
+           * The call might not yield result the user intended.
+           */
+          g_string_append (result_string, "1");
         }
+      break;
 
+    case SF_ENUM:
+      if (G_VALUE_HOLDS_ENUM (gvalue))
+        {
+          /* Effectively upcasting to a less restrictive Scheme class Integer. */
+          g_string_append_printf (result_string, "%d", g_value_get_enum (gvalue));
+        }
+      else
+        {
+          /* For now, occurs when GimpConfig or GimpProcedureDialog does not support GParamEnum. */
+          g_warning ("Expecting GValue holding a GEnum.");
+          /* Append arbitrary int, so no errors in signature of Scheme call.
+           * The call might not yield result the user intended.
+           */
+          g_string_append (result_string, "1");
+        }
       break;
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]