[PATCH v3] make function rtnl_link_set_name() more reasonable

Hongwei Bi hwbi2008 at gmail.com
Thu Jan 23 11:10:53 EST 2014


We set the maximum value of IFNAMSIZ is 16 bytes. So when the length of
the new link's name is equal or greater  then 16, we should notify the user
that the length of the name has been truncated to 15 bytes.

Signed-off-by: Hongwei Bi <hwbi2008 at gmail.com>
---
 include/netlink/route/link.h |  2 +-
 lib/route/link.c             | 12 +++++++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/include/netlink/route/link.h b/include/netlink/route/link.h
index 3345787..566e224 100644
--- a/include/netlink/route/link.h
+++ b/include/netlink/route/link.h
@@ -151,7 +151,7 @@ extern int	rtnl_link_str2carrier(const char *);
 extern void	rtnl_link_set_qdisc(struct rtnl_link *, const char *);
 extern char *	rtnl_link_get_qdisc(struct rtnl_link *);
 
-extern void	rtnl_link_set_name(struct rtnl_link *, const char *);
+extern int	rtnl_link_set_name(struct rtnl_link *, const char *);
 extern char *	rtnl_link_get_name(struct rtnl_link *);
 
 extern void	rtnl_link_set_group(struct rtnl_link *, uint32_t);
diff --git a/lib/route/link.c b/lib/route/link.c
index 9979b5b..6a59c14 100644
--- a/lib/route/link.c
+++ b/lib/route/link.c
@@ -1651,11 +1651,21 @@ void rtnl_link_put(struct rtnl_link *link)
  * @route_doc{link_attr_name, Link Name}
  * @see rtnl_link_get_name()
  * @see rtnl_link_set_ifindex()
+ *
+ *@return -1 when the length of the name is equal or greater than IFNAMSIZ.
+ *	In this case,the length of the name has been truncated to 15 bytes. 
+ *@return 0 normally
  */
-void rtnl_link_set_name(struct rtnl_link *link, const char *name)
+int rtnl_link_set_name(struct rtnl_link *link, const char *name)
 {
+	int err = 0;
+	if (strlen(name) >= IFNAMSIZ)
+		err = -1;
+
 	strncpy(link->l_name, name, sizeof(link->l_name) - 1);
 	link->ce_mask |= LINK_ATTR_IFNAME;
+
+	return err;
 }
 
 /**
-- 
1.8.1.2




More information about the libnl mailing list