]> granicus.if.org Git - libnl/commitdiff
route/link: avoid dangling pointer in rtnl_link_set_slave_type()
authorThomas Haller <thaller@redhat.com>
Thu, 7 Mar 2019 10:28:14 +0000 (11:28 +0100)
committerThomas Haller <thaller@redhat.com>
Thu, 7 Mar 2019 10:50:52 +0000 (11:50 +0100)
- 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.

lib/route/link.c

index 7dc36da4894a2d0dc6d577c92920a42571d3da57..128607cd1623591e49c8dd1b2c21b422e5058098 100644 (file)
@@ -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;
 }