[glib] gapplication: reject actions without names



commit ee718d352615f8417683f99fba6d0d08a33760fa
Author: Lars Uebernickel <lars uebernickel canonical com>
Date:   Wed Oct 7 15:49:05 2015 +0200

    gapplication: reject actions without names
    
    https://bugzilla.gnome.org/show_bug.cgi?id=756134

 gio/gsimpleaction.c      |    2 ++
 gio/gsimpleactiongroup.c |    7 +++++++
 gio/tests/gapplication.c |   25 +++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 0 deletions(-)
---
diff --git a/gio/gsimpleaction.c b/gio/gsimpleaction.c
index e9eb360..2c0bc85 100644
--- a/gio/gsimpleaction.c
+++ b/gio/gsimpleaction.c
@@ -605,6 +605,8 @@ GSimpleAction *
 g_simple_action_new (const gchar        *name,
                      const GVariantType *parameter_type)
 {
+  g_return_val_if_fail (name != NULL, NULL);
+
   return g_object_new (G_TYPE_SIMPLE_ACTION,
                        "name", name,
                        "parameter-type", parameter_type,
diff --git a/gio/gsimpleactiongroup.c b/gio/gsimpleactiongroup.c
index c542251..c62e66f 100644
--- a/gio/gsimpleactiongroup.c
+++ b/gio/gsimpleactiongroup.c
@@ -191,6 +191,13 @@ g_simple_action_group_add_action (GActionMap *action_map,
   GAction *old_action;
 
   action_name = g_action_get_name (action);
+  if (action_name == NULL)
+    {
+      g_critical ("The supplied action has no name. You must set the "
+                  "GAction:name property when creating an action.");
+      return;
+    }
+
   old_action = g_hash_table_lookup (simple->priv->table, action_name);
 
   if (old_action != action)
diff --git a/gio/tests/gapplication.c b/gio/tests/gapplication.c
index 42f76af..33d757e 100644
--- a/gio/tests/gapplication.c
+++ b/gio/tests/gapplication.c
@@ -937,6 +937,30 @@ test_handle_local_options_passthrough (void)
   g_test_trap_assert_passed ();
 }
 
+static void
+test_api (void)
+{
+  GApplication *app;
+  GSimpleAction *action;
+
+  app = g_application_new ("org.gtk.TestApplication", 0);
+
+  /* add an action without a name */
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*assertion*failed*");
+  action = g_simple_action_new (NULL, NULL);
+  g_assert (action == NULL);
+  g_test_assert_expected_messages ();
+
+  /* also, gapplication shouldn't accept actions without names */
+  action = g_object_new (G_TYPE_SIMPLE_ACTION, NULL);
+  g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*action has no name*");
+  g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (action));
+  g_test_assert_expected_messages ();
+
+  g_object_unref (action);
+  g_object_unref (app);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -961,6 +985,7 @@ main (int argc, char **argv)
   g_test_add_func ("/gapplication/test-handle-local-options1", test_handle_local_options_success);
   g_test_add_func ("/gapplication/test-handle-local-options2", test_handle_local_options_failure);
   g_test_add_func ("/gapplication/test-handle-local-options3", test_handle_local_options_passthrough);
+  g_test_add_func ("/gapplication/api", test_api);
 
   return g_test_run ();
 }


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