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.
This commit is contained in:
Thomas Haller 2015-10-27 10:25:22 +01:00
parent 99f9785345
commit 9369fac8ca
8 changed files with 52 additions and 39 deletions

View file

@ -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),

View file

@ -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.

View file

@ -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,

View file

@ -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));

View file

@ -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));

View file

@ -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);

View file

@ -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",

View file

@ -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",