summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-03-07 11:28:14 +0100
committerThomas Haller <thaller@redhat.com>2019-03-07 11:50:52 +0100
commitcf6804add6afa94707811f48f391be3ac28b45d6 (patch)
tree5c861b131f4694bdcc6319d7e177d8ffe862a1b3 /lib
parentc503f924a09746fd4f6909891918b853ca0b09d9 (diff)
downloadlibnl-cf6804add6afa94707811f48f391be3ac28b45d6.tar.gz
route/link: avoid dangling pointer in rtnl_link_set_slave_type()
- 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.
Diffstat (limited to 'lib')
-rw-r--r--lib/route/link.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/lib/route/link.c b/lib/route/link.c
index 7dc36da4..128607cd 100644
--- a/lib/route/link.c
+++ b/lib/route/link.c
@@ -2623,21 +2623,21 @@ char *rtnl_link_get_type(struct rtnl_link *link)
*/
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;
}