From 7b6ffb1e5f00f644363cde94d20f679761663fd6 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 29 Apr 2015 09:19:15 +0200 Subject: [PATCH] platform: intern driver string for NMPlatformLink Always intern string from udev_get_driver(). We use the result of udev_get_driver() for setting NMPlatformLink.driver. In all other cases, we already set that value to an interned string, which simplifies memory handling. As it was, the lifetime of that string was tied to the lifetime of the GUdevDevice. This is not a stelar solution, but we assume that the overall numbers of different drivers is limited so we don't leak large amounts of memory. (cherry picked from commit 3171b543dc30090fc50d0c74786e76062e079ed2) --- src/platform/nm-linux-platform.c | 17 ++++++----------- src/platform/nm-platform.c | 2 +- src/platform/nm-platform.h | 8 +++++++- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index df8456e885..126b01f867 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -455,7 +455,7 @@ udev_get_driver (GUdevDevice *device, int ifindex) driver = g_udev_device_get_driver (device); if (driver) - return driver; + goto out; /* Try the parent */ parent = g_udev_device_get_parent (device); @@ -470,23 +470,18 @@ udev_get_driver (GUdevDevice *device, int ifindex) if ( (g_strcmp0 (subsys, "ibmebus") == 0) || (subsys == NULL)) { grandparent = g_udev_device_get_parent (parent); - if (grandparent) { + if (grandparent) driver = g_udev_device_get_driver (grandparent); - } } } } - - /* Intern the string so we don't have to worry about memory - * management in NMPlatformLink. - */ - if (driver) - driver = g_intern_string (driver); - g_clear_object (&parent); g_clear_object (&grandparent); - return driver; +out: + /* Intern the string so we don't have to worry about memory + * management in NMPlatformLink. */ + return g_intern_string (driver); } /****************************************************************** diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index 3d00ae4ae4..9b1d64e7ff 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -2856,7 +2856,7 @@ nm_platform_link_cmp (const NMPlatformLink *a, const NMPlatformLink *b) _CMP_FIELD_BOOL (a, b, initialized); _CMP_FIELD_STR_INTERNED (a, b, kind); _CMP_FIELD_STR0 (a, b, udi); - _CMP_FIELD_STR0 (a, b, driver); + _CMP_FIELD_STR_INTERNED (a, b, driver); return 0; } diff --git a/src/platform/nm-platform.h b/src/platform/nm-platform.h index 43b9731f5b..8d914c5e3b 100644 --- a/src/platform/nm-platform.h +++ b/src/platform/nm-platform.h @@ -86,11 +86,17 @@ struct _NMPlatformLink { char name[IFNAMSIZ]; NMLinkType type; - /* rtnl_link_get_type(), IFLA_INFO_KIND */ + /* rtnl_link_get_type(), IFLA_INFO_KIND. */ + /* NMPlatform initializes this field with a static string. */ const char *kind; + /* Beware: NMPlatform initializes this string with an allocated string. + * Handle it properly (i.e. don't keep a reference to it). */ const char *udi; + + /* NMPlatform initializes this field with a static string. */ const char *driver; + gboolean initialized; int master; int parent;