summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-07-03 20:13:41 +0200
committerThomas Haller <thaller@redhat.com>2020-07-08 12:12:43 +0200
commit107cfeb71a2dd8376be6618792c79f2404bb4541 (patch)
treec8de3e974f6760cee5db400f149cb3da38ba4464
parentf7c519b1a3c47cf55494acf94db751189af91093 (diff)
downloadNetworkManager-th/libnm_jansson-2.tar.gz
shared: cleanup dlopening libjansson depending on configure optionsth/libnm_jansson-2
- assert that WITH_JANSSON and JANSSON_SONAME are defined consistently. This check ensures that we can check at compile time that nm_json_vt() will always fail (because JANSSON_SONAME) is undefined. That is interesting, because this means you can do a compile time for !WITH_JANSSON, and know if nm_json_vt() will *never* succeed. With that, we could let the linker know when the code is unused and remove all uses of nm_json_vt(), without using the traditional conditional compilation with "#if WITH_JANSSON". But of course, we currently don't do this micro optimization to remove defunct code. - drop the "mode" helper variable and pass the flags directly to dlopen().
-rw-r--r--libnm-core/tests/test-setting.c19
-rw-r--r--shared/nm-glib-aux/nm-json-aux.c32
2 files changed, 38 insertions, 13 deletions
diff --git a/libnm-core/tests/test-setting.c b/libnm-core/tests/test-setting.c
index 5ec0ccbdb1..1b53baeda3 100644
--- a/libnm-core/tests/test-setting.c
+++ b/libnm-core/tests/test-setting.c
@@ -971,6 +971,24 @@ test_dcb_bandwidth_sums (void)
/*****************************************************************************/
static void
+test_nm_json (void)
+{
+ g_assert (NM_IN_SET (WITH_JANSSON, 0, 1));
+
+#if WITH_JANSSON
+ g_assert (nm_json_vt ());
+#else
+ g_assert (!nm_json_vt ());
+#endif
+
+#if WITH_JANSSON != defined (JANSSON_SONAME)
+#error "WITH_JANSON and JANSSON_SONAME are defined inconsistently."
+#endif
+}
+
+/*****************************************************************************/
+
+static void
_test_team_config_sync (const char *team_config,
int notify_peer_count,
int notify_peers_interval,
@@ -4087,6 +4105,7 @@ main (int argc, char **argv)
g_test_add_func ("/libnm/settings/bridge/vlans", test_bridge_vlans);
g_test_add_func ("/libnm/settings/bridge/verify", test_bridge_verify);
+ g_test_add_func ("/libnm/test_nm_json", test_nm_json);
g_test_add_func ("/libnm/settings/team/sync_runner_from_config_roundrobin",
test_runner_roundrobin_sync_from_config);
g_test_add_func ("/libnm/settings/team/sync_runner_from_config_broadcast",
diff --git a/shared/nm-glib-aux/nm-json-aux.c b/shared/nm-glib-aux/nm-json-aux.c
index aba979d205..4758d2a1bd 100644
--- a/shared/nm-glib-aux/nm-json-aux.c
+++ b/shared/nm-glib-aux/nm-json-aux.c
@@ -147,26 +147,32 @@ static NMJsonVtInternal *
_nm_json_vt_internal_load (void)
{
NMJsonVtInternal *v;
- void *handle = NULL;
- int mode;
+ const char *soname;
+ void *handle;
v = g_new (NMJsonVtInternal, 1);
-
*v = (NMJsonVtInternal) { };
-#ifndef JANSSON_SONAME
-#define JANSSON_SONAME ""
-#endif
-
- mode = RTLD_LAZY | RTLD_LOCAL | RTLD_NODELETE | RTLD_DEEPBIND;
-#if defined (ASAN_BUILD)
- /* Address sanitizer is incompatible with RTLD_DEEPBIND. */
- mode &= ~RTLD_DEEPBIND;
+#if WITH_JANSSON && defined (JANSSON_SONAME)
+ G_STATIC_ASSERT_EXPR (NM_STRLEN (JANSSON_SONAME) > 0);
+ nm_assert (strlen (JANSSON_SONAME) > 0);
+ soname = JANSSON_SONAME;
+#elif !WITH_JANSSON && !defined (JANSSON_SONAME)
+ soname = NULL;
+#else
+#error "WITH_JANSON and JANSSON_SONAME are defined inconsistently."
#endif
- if (strlen (JANSSON_SONAME) > 0)
- handle = dlopen (JANSSON_SONAME, mode);
+ if (!soname)
+ return v;
+ handle = dlopen (soname, RTLD_LAZY
+ | RTLD_LOCAL
+ | RTLD_NODELETE
+#if !defined (ASAN_BUILD)
+ | RTLD_DEEPBIND
+#endif
+ | 0);
if (!handle)
return v;