- don't leave a dangling pointer, in case we unset the
kind.
- try first to clone the string. If that fails, return early
without modifying the link. Only start modifying the link,
after we know it's going to succeed.
*/
int rtnl_link_set_slave_type(struct rtnl_link *link, const char *type)
{
- char *kind;
-
- free(link->l_info_slave_kind);
- link->ce_mask &= ~LINK_ATTR_LINKINFO_SLAVE_KIND;
+ char *kind = NULL;
- if (!type)
- return 0;
-
- kind = strdup(type);
- if (!kind)
- return -NLE_NOMEM;
+ if (type) {
+ kind = strdup(type);
+ if (!kind)
+ return -NLE_NOMEM;
+ }
+ free(link->l_info_slave_kind);
link->l_info_slave_kind = kind;
- link->ce_mask |= LINK_ATTR_LINKINFO_SLAVE_KIND;
+ if (kind)
+ link->ce_mask |= LINK_ATTR_LINKINFO_SLAVE_KIND;
+ else
+ link->ce_mask &= ~LINK_ATTR_LINKINFO_SLAVE_KIND;
return 0;
}