From 7a2ae46ecf67583ac663c81a2424d3abd61b741b Mon Sep 17 00:00:00 2001 From: xiangxistu Date: Thu, 25 Feb 2021 14:06:03 +0800 Subject: [PATCH] [fix] the wrong 'ifconfig' information for multi-network interface --- components/net/netdev/src/netdev.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/components/net/netdev/src/netdev.c b/components/net/netdev/src/netdev.c index 76a937cc75..d7bcf05654 100644 --- a/components/net/netdev/src/netdev.c +++ b/components/net/netdev/src/netdev.c @@ -45,6 +45,7 @@ struct netdev *netdev_default; int netdev_register(struct netdev *netdev, const char *name, void *user_data) { rt_base_t level; + uint8_t name_len; uint16_t flags_mask; int index; @@ -77,8 +78,21 @@ int netdev_register(struct netdev *netdev, const char *name, void *user_data) netdev->status_callback = RT_NULL; netdev->addr_callback = RT_NULL; + if(rt_strlen(name) > RT_NAME_MAX) + { + char netdev_name[RT_NAME_MAX + 1] = {0}; + + name_len = RT_NAME_MAX; + rt_strncpy(netdev_name, name, name_len); + LOG_E("netdev name[%s] length is so long that have been cut into [%s].", name, netdev_name); + } + else + { + name_len = rt_strlen(name); + } + /* fill network interface device */ - rt_strncpy(netdev->name, name, RT_NAME_MAX); + rt_strncpy(netdev->name, name, name_len); netdev->user_data = user_data; /* initialize current network interface device single list */ @@ -260,7 +274,7 @@ struct netdev *netdev_get_by_name(const char *name) for (node = &(netdev_list->list); node; node = rt_slist_next(node)) { netdev = rt_slist_entry(node, struct netdev, list); - if (netdev && (rt_strncmp(netdev->name, name, RT_NAME_MAX) == 0)) + if (netdev && (rt_strncmp(netdev->name, name, rt_strlen(netdev->name) < RT_NAME_MAX ? rt_strlen(netdev->name) : RT_NAME_MAX) == 0)) { rt_hw_interrupt_enable(level); return netdev;