[PATCH] make function rtnl_link_set_name() more reasonable

Thomas Graf tgraf at suug.ch
Tue Jan 7 08:02:33 EST 2014


On 01/02/14 at 12:28am, Hongwei Bi wrote:
> We set the maximum value of IFNAMSIZ is 16 bytes.So when
> the size of the new link's name is longer then 16,we should
> at least notify the user that the name's size is invalid
> instead of cutting off the extra parts simply.
> 
> Signed-off-by: Hongwei Bi <hwbi2008 at gmail.com>
> ---
>  include/netlink/route/link.h | 2 +-
>  lib/route/link.c             | 6 +++++-
>  2 files changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/include/netlink/route/link.h b/include/netlink/route/link.h
> index 29a5df9..472e057 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 *);

Usually this would break ABI but changing void to int seems to be OK
from an ABI and API stability perspective.

>  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 4bcf615..0bb7c55 100644
> --- a/lib/route/link.c
> +++ b/lib/route/link.c
> @@ -1652,10 +1652,14 @@ void rtnl_link_put(struct rtnl_link *link)
>   * @see rtnl_link_get_name()
>   * @see rtnl_link_set_ifindex()
>   */
> -void rtnl_link_set_name(struct rtnl_link *link, const char *name)
> +int rtnl_link_set_name(struct rtnl_link *link, const char *name)
>  {
> +	if(strlen(name) > IFNAMSIZ)
> +		return -NLE_INVAL;
> +
>  	strncpy(link->l_name, name, sizeof(link->l_name) - 1);
>  	link->ce_mask |= LINK_ATTR_IFNAME;
> +	return 0; 

ACK



More information about the libnl mailing list