gplugin/gplugin

Guard against double gplugin_init()
develop
2018-10-21, Mike Ruprecht
e621cbd18071
Parents 0fa031c839de
Children 18cab3835ee1
Guard against double gplugin_init()

This patch guards against the GPlugin manager instance getting
stomped if gplugin_init() is called twice. This is important as if
a library calls gplugin_init() and a program using said library needs
to parse arguments or otherwise use GPlugin API prior to that call,
there would be a second manager instance created calling
gplugin_init() again or an assertion when trying to parse the
arguments without first initializing GPlugin.
--- a/gplugin/gplugin-manager.c Sun Oct 21 14:56:15 2018 -0500
+++ b/gplugin/gplugin-manager.c Sun Oct 21 15:06:19 2018 -0500
@@ -1084,6 +1084,10 @@
*****************************************************************************/
void
gplugin_manager_private_init(void) {
+ if (instance != NULL) {
+ return;
+ }
+
instance = g_object_new(GPLUGIN_TYPE_MANAGER, NULL);
gplugin_manager_register_loader(GPLUGIN_TYPE_NATIVE_LOADER);
@@ -1095,7 +1099,7 @@
gplugin_manager_private_uninit(void) {
g_regex_unref(dependency_regex);
- g_object_unref(G_OBJECT(instance));
+ g_clear_object(&instance);
}
/******************************************************************************
--- a/gplugin/tests/test-core.c Sun Oct 21 14:56:15 2018 -0500
+++ b/gplugin/tests/test-core.c Sun Oct 21 15:06:19 2018 -0500
@@ -29,6 +29,13 @@
}
static void
+test_gplugin_init_init_uninit(void) {
+ gplugin_init();
+ gplugin_init();
+ gplugin_uninit();
+}
+
+static void
test_gplugin_init_uninit_with_refresh(void) {
gplugin_init();
gplugin_manager_refresh();
@@ -96,6 +103,8 @@
g_test_add_func("/core/init_uninit",
test_gplugin_init_uninit);
+ g_test_add_func("/core/init_init_uninit",
+ test_gplugin_init_init_uninit);
g_test_add_func("/core/init_uninit_with_refresh",
test_gplugin_init_uninit_with_refresh);
g_test_add_func("/core/init_uninit_with_refresh_plugins",