diff options
author | Thomas Haller <thaller@redhat.com> | 2015-10-27 10:25:22 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-11-02 13:57:01 +0100 |
commit | 9369fac8ca18f47541526068e83b652646153c18 (patch) | |
tree | a934469afdd2d9bbe2f33b14b77605512c89f70f | |
parent | 99f9785345d3c65509da033c1f7c0d6a14a85e6b (diff) | |
download | NetworkManager-9369fac8ca18f47541526068e83b652646153c18.tar.gz |
core: add nm_utils_to_string_buffer global buffer
Having a static string buffer for convenience is useful not only
for platform. Define the string buffer in NetworkManagerUtils.h,
so that all to-string functions can reuse *one* buffer.
Of course, this has the potential danger, that different
to-string method might reuse the same buffer. Hence, low-level
library functions are adviced to use their own buffer, because
an upper level might already use the global buffer for another
string.
-rw-r--r-- | include/nm-test-utils.h | 4 | ||||
-rw-r--r-- | src/NetworkManagerUtils.c | 26 | ||||
-rw-r--r-- | src/NetworkManagerUtils.h | 5 | ||||
-rw-r--r-- | src/platform/nm-linux-platform.c | 4 | ||||
-rw-r--r-- | src/platform/nm-platform.c | 36 | ||||
-rw-r--r-- | src/platform/nm-platform.h | 2 | ||||
-rw-r--r-- | src/platform/nmp-object.c | 12 | ||||
-rw-r--r-- | src/tests/test-route-manager.c | 2 |
8 files changed, 52 insertions, 39 deletions
diff --git a/include/nm-test-utils.h b/include/nm-test-utils.h index 85a6e78385..9f40be9c12 100644 --- a/include/nm-test-utils.h +++ b/include/nm-test-utils.h @@ -983,7 +983,7 @@ nmtst_platform_ip4_routes_equal (const NMPlatformIP4Route *a, const NMPlatformIP for (i = 0; i < len; i++) { if (nm_platform_ip4_route_cmp (&a[i], &b[i]) != 0) { - char buf[sizeof (_nm_platform_to_string_buffer)]; + char buf[sizeof (_nm_utils_to_string_buffer)]; g_error ("Error comparing IPv4 route[%lu]: %s vs %s", (long unsigned) i, nm_platform_ip4_route_to_string (&a[i], NULL, 0), @@ -1017,7 +1017,7 @@ nmtst_platform_ip6_routes_equal (const NMPlatformIP6Route *a, const NMPlatformIP for (i = 0; i < len; i++) { if (nm_platform_ip6_route_cmp (&a[i], &b[i]) != 0) { - char buf[sizeof (_nm_platform_to_string_buffer)]; + char buf[sizeof (_nm_utils_to_string_buffer)]; g_error ("Error comparing IPv6 route[%lu]: %s vs %s", (long unsigned) i, nm_platform_ip6_route_to_string (&a[i], NULL, 0), diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 726ceb9b4a..b2d98ca58d 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -1725,6 +1725,32 @@ nm_match_spec_join (GSList *specs) return g_string_free (str, FALSE); } +/*****************************************************************************/ + +char _nm_utils_to_string_buffer[]; + +void +nm_utils_to_string_buffer_init (char **buf, gsize *len) +{ + if (!*buf) { + *buf = _nm_utils_to_string_buffer; + *len = sizeof (_nm_utils_to_string_buffer); + } +} + +gboolean +nm_utils_to_string_buffer_init_null (gconstpointer obj, char **buf, gsize *len) +{ + nm_utils_to_string_buffer_init (buf, len); + if (!obj) { + g_strlcpy (*buf, "(null)", *len); + return FALSE; + } + return TRUE; +} + +/*****************************************************************************/ + /** * nm_utils_get_shared_wifi_permission: * @connection: the NMConnection to lookup the permission. diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index bf90e741cd..b4cc180013 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -191,6 +191,11 @@ NMMatchSpecMatchType nm_match_spec_match_config (const GSList *specs, guint nm_v GSList *nm_match_spec_split (const char *value); char *nm_match_spec_join (GSList *specs); +extern char _nm_utils_to_string_buffer[2096]; + +void nm_utils_to_string_buffer_init (char **buf, gsize *len); +gboolean nm_utils_to_string_buffer_init_null (gconstpointer obj, char **buf, gsize *len); + const char *nm_utils_get_shared_wifi_permission (NMConnection *connection); const char *nm_utils_get_ip_config_method (NMConnection *connection, diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index 2c60b8419e..b092e080b1 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -2391,8 +2391,8 @@ cache_pre_hook (NMPCache *cache, const NMPObject *old, const NMPObject *new, NMP NMPlatform *platform = NM_PLATFORM (user_data); NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform); const NMPClass *klass; - char str_buf[sizeof (_nm_platform_to_string_buffer)]; - char str_buf2[sizeof (_nm_platform_to_string_buffer)]; + char str_buf[sizeof (_nm_utils_to_string_buffer)]; + char str_buf2[sizeof (_nm_utils_to_string_buffer)]; nm_assert (old || new); nm_assert (NM_IN_SET (ops_type, NMP_CACHE_OPS_ADDED, NMP_CACHE_OPS_REMOVED, NMP_CACHE_OPS_UPDATED)); diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 4827638137..480b07e18a 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -2477,22 +2477,6 @@ _lifetime_summary_to_string (gint32 now, guint32 timestamp, guint32 preferred, g return buf; } -char _nm_platform_to_string_buffer[]; - -static gboolean -_to_string_buffer_init (gconstpointer obj, char **buf, gsize *len) -{ - if (!*buf) { - *buf = _nm_platform_to_string_buffer; - *len = sizeof (_nm_platform_to_string_buffer); - } - if (!obj) { - g_strlcpy (*buf, "(null)", *len); - return FALSE; - } - return TRUE; -} - /** * nm_platform_link_to_string: * @route: pointer to NMPlatformLink address structure @@ -2514,7 +2498,7 @@ nm_platform_link_to_string (const NMPlatformLink *link, char *buf, gsize len) gs_free char *str_inet6_token = NULL; const char *str_link_type; - if (!_to_string_buffer_init (link, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (link, &buf, &len)) return buf; str_flags = g_string_new (NULL); @@ -2623,7 +2607,7 @@ nm_platform_lnk_gre_to_string (const NMPlatformLnkGre *lnk, char *buf, gsize len char str_output_key[30]; char str_output_key1[NM_UTILS_INET_ADDRSTRLEN]; - if (!_to_string_buffer_init (lnk, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (lnk, &buf, &len)) return buf; g_snprintf (buf, len, @@ -2657,7 +2641,7 @@ nm_platform_lnk_infiniband_to_string (const NMPlatformLnkInfiniband *lnk, char * { char str_p_key[64]; - if (!_to_string_buffer_init (lnk, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (lnk, &buf, &len)) return buf; g_snprintf (buf, len, @@ -2674,7 +2658,7 @@ nm_platform_lnk_infiniband_to_string (const NMPlatformLnkInfiniband *lnk, char * const char * nm_platform_lnk_macvlan_to_string (const NMPlatformLnkMacvlan *lnk, char *buf, gsize len) { - if (!_to_string_buffer_init (lnk, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (lnk, &buf, &len)) return buf; g_snprintf (buf, len, @@ -2688,7 +2672,7 @@ nm_platform_lnk_macvlan_to_string (const NMPlatformLnkMacvlan *lnk, char *buf, g const char * nm_platform_lnk_vlan_to_string (const NMPlatformLnkVlan *lnk, char *buf, gsize len) { - if (!_to_string_buffer_init (lnk, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (lnk, &buf, &len)) return buf; g_snprintf (buf, len, "vlan %u", (guint) lnk->id); @@ -2709,7 +2693,7 @@ nm_platform_lnk_vxlan_to_string (const NMPlatformLnkVxlan *lnk, char *buf, gsize char str_tos[25]; char str_ttl[25]; - if (!_to_string_buffer_init (lnk, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (lnk, &buf, &len)) return buf; if (lnk->group == 0) @@ -2806,7 +2790,7 @@ nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address, char *bu const char *str_lft_p, *str_pref_p, *str_time_p; gint32 now = nm_utils_get_monotonic_timestamp_s (); - if (!_to_string_buffer_init (address, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (address, &buf, &len)) return buf; inet_ntop (AF_INET, &address->address, s_address, sizeof (s_address)); @@ -2912,7 +2896,7 @@ nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address, char *bu const char *str_lft_p, *str_pref_p, *str_time_p; gint32 now = nm_utils_get_monotonic_timestamp_s (); - if (!_to_string_buffer_init (address, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (address, &buf, &len)) return buf; inet_ntop (AF_INET6, &address->address, s_address, sizeof (s_address)); @@ -2971,7 +2955,7 @@ nm_platform_ip4_route_to_string (const NMPlatformIP4Route *route, char *buf, gsi char str_dev[TO_STRING_DEV_BUF_SIZE]; char str_scope[30]; - if (!_to_string_buffer_init (route, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (route, &buf, &len)) return buf; inet_ntop (AF_INET, &route->network, s_network, sizeof(s_network)); @@ -3020,7 +3004,7 @@ nm_platform_ip6_route_to_string (const NMPlatformIP6Route *route, char *buf, gsi char s_network[INET6_ADDRSTRLEN], s_gateway[INET6_ADDRSTRLEN]; char str_dev[TO_STRING_DEV_BUF_SIZE]; - if (!_to_string_buffer_init (route, &buf, &len)) + if (!nm_utils_to_string_buffer_init_null (route, &buf, &len)) return buf; inet_ntop (AF_INET6, &route->network, s_network, sizeof(s_network)); diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index 2522f3092f..2c6831e32d 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -770,8 +770,6 @@ gboolean nm_platform_ip6_route_add (NMPlatform *self, int ifindex, NMIPConfigSou gboolean nm_platform_ip4_route_delete (NMPlatform *self, int ifindex, in_addr_t network, int plen, guint32 metric); gboolean nm_platform_ip6_route_delete (NMPlatform *self, int ifindex, struct in6_addr network, int plen, guint32 metric); -extern char _nm_platform_to_string_buffer[1024]; - const char *nm_platform_link_to_string (const NMPlatformLink *link, char *buf, gsize len); const char *nm_platform_lnk_gre_to_string (const NMPlatformLnkGre *lnk, char *buf, gsize len); const char *nm_platform_lnk_infiniband_to_string (const NMPlatformLnkInfiniband *lnk, char *buf, gsize len); diff --git a/src/platform/nmp-object.c b/src/platform/nmp-object.c index a783021d83..ae09332736 100644 --- a/src/platform/nmp-object.c +++ b/src/platform/nmp-object.c @@ -379,13 +379,13 @@ const char * nmp_object_to_string (const NMPObject *obj, NMPObjectToStringMode to_string_mode, char *buf, gsize buf_size) { const NMPClass *klass; - char buf2[sizeof (_nm_platform_to_string_buffer)]; - char buf3[sizeof (_nm_platform_to_string_buffer)]; - char buf4[sizeof (_nm_platform_to_string_buffer)]; + char buf2[sizeof (_nm_utils_to_string_buffer)]; + char buf3[sizeof (_nm_utils_to_string_buffer)]; + char buf4[sizeof (_nm_utils_to_string_buffer)]; if (!buf) { - buf = _nm_platform_to_string_buffer; - buf_size = sizeof (_nm_platform_to_string_buffer); + buf = _nm_utils_to_string_buffer; + buf_size = sizeof (_nm_utils_to_string_buffer); } if (!obj) { @@ -413,7 +413,7 @@ nmp_object_to_string (const NMPObject *obj, NMPObjectToStringMode to_string_mode obj->_link.netlink.is_in_netlink ? '+' : '-', obj->_link.udev.device); if (obj->_link.netlink.lnk) { - char b[sizeof (_nm_platform_to_string_buffer)]; + char b[sizeof (_nm_utils_to_string_buffer)]; g_snprintf (buf4, sizeof (buf4), ", link:%s, %s", diff --git a/src/tests/test-route-manager.c b/src/tests/test-route-manager.c index 09862d2368..eba7d9ef77 100644 --- a/src/tests/test-route-manager.c +++ b/src/tests/test-route-manager.c @@ -797,7 +797,7 @@ _assert_route_check (const NMPlatformVTableRoute *vtable, gboolean has, const NM if (!has) { g_assert (!r); } else { - char buf[sizeof (_nm_platform_to_string_buffer)]; + char buf[sizeof (_nm_utils_to_string_buffer)]; if (!r || vtable->route_cmp (route, r) != 0) g_error ("Invalid route. Expect %s, has %s", |