diff options
author | Thomas Haller <thaller@redhat.com> | 2019-03-07 11:28:14 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-03-07 11:50:52 +0100 |
commit | cf6804add6afa94707811f48f391be3ac28b45d6 (patch) | |
tree | 5c861b131f4694bdcc6319d7e177d8ffe862a1b3 /lib | |
parent | c503f924a09746fd4f6909891918b853ca0b09d9 (diff) | |
download | libnl-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.c | 22 |
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; } |