core/lldp: minor cleanup in _lldp_attr_*()

- use nm_g_variant_unref_floating()

- rename _lldp_attr_take_str_ptr() to _lldp_attr_set_str_take().
  The new name has the same "_lldp_attr_set_" prefix as other setters.
  Also, with the previous name it is unclear why it takes a "str-ptr".

- setting the same attribute multiple times, ignores all but the first
  value. Avoid cloning the string in that case, and explicitly choose
  the set or take function.

(cherry picked from commit 0fbb54839e)
This commit is contained in:
Thomas Haller
2019-08-01 15:05:27 +02:00
parent 273f0b5416
commit d84d1db39e

View File

@@ -224,7 +224,7 @@ _lldp_attr_set_str (LldpAttrData *pdata, LldpAttrId attr_id, const char *v_strin
}
static void
_lldp_attr_take_str_ptr (LldpAttrData *pdata, LldpAttrId attr_id, char *str)
_lldp_attr_set_str_take (LldpAttrData *pdata, LldpAttrId attr_id, char *str)
{
nm_assert (pdata);
nm_assert (_lldp_attr_id_to_type (attr_id) == LLDP_ATTR_TYPE_STRING);
@@ -267,8 +267,7 @@ _lldp_attr_set_vardict (LldpAttrData *pdata, LldpAttrId attr_id, GVariant *varia
/* we ignore duplicate fields silently */
if (pdata->attr_type != LLDP_ATTR_TYPE_NONE) {
if (g_variant_is_floating (variant))
g_variant_unref (variant);
nm_g_variant_unref_floating (variant);
return;
}
@@ -687,9 +686,10 @@ lldp_neighbor_new (sd_lldp_neighbor *neighbor_sd, GError **error)
g_variant_dict_end (&dict));
_lldp_attr_set_uint32 (neigh->attrs, LLDP_ATTR_ID_IEEE_802_1_VID, vid);
_lldp_attr_take_str_ptr (neigh->attrs,
LLDP_ATTR_ID_IEEE_802_1_VLAN_NAME,
name_to_free ?: g_strdup (name));
if (name_to_free)
_lldp_attr_set_str_take (neigh->attrs, LLDP_ATTR_ID_IEEE_802_1_VLAN_NAME, name_to_free);
else
_lldp_attr_set_str (neigh->attrs, LLDP_ATTR_ID_IEEE_802_1_VLAN_NAME, name);
break;
}
default: