Better to use the return value of rte_eth_dev_get_name_by_port()
rather than depending on side effect of name being set or not.

Coverity issue: 511674
Fixes: 05f1d6842fc3 ("examples/multi_process: add hotplug sample")

Signed-off-by: Stephen Hemminger <[email protected]>
---
v2 - fix printf format typo, and commit message

 examples/multi_process/hotplug_mp/commands.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/examples/multi_process/hotplug_mp/commands.c 
b/examples/multi_process/hotplug_mp/commands.c
index e84e130b3c..7179a5bf8e 100644
--- a/examples/multi_process/hotplug_mp/commands.c
+++ b/examples/multi_process/hotplug_mp/commands.c
@@ -2,6 +2,8 @@
  * Copyright(c) 2018-2023 Intel Corporation.
  */
 
+#include <string.h>
+
 #include <rte_bus.h>
 #include <rte_ethdev.h>
 #include "commands.h"
@@ -35,13 +37,15 @@ cmd_list_parsed(__rte_unused void *parsed_result,
        cmdline_printf(cl, "list all etherdev\n");
 
        RTE_ETH_FOREACH_DEV(port_id) {
-               char dev_name[RTE_DEV_NAME_MAX_LEN] = "";
+               char dev_name[RTE_DEV_NAME_MAX_LEN];
+               int ret;
 
-               rte_eth_dev_get_name_by_port(port_id, dev_name, 
sizeof(dev_name));
-               if (strlen(dev_name) > 0)
-                       cmdline_printf(cl, "%d\t%s\n", port_id, dev_name);
+               ret = rte_eth_dev_get_name_by_port(port_id, dev_name, 
sizeof(dev_name));
+               if (ret == 0)
+                       cmdline_printf(cl, "%u\t%s\n", port_id, dev_name);
                else
-                       printf("empty dev_name is not expected!\n");
+                       cmdline_printf(cl, "No name for port %u: %s\n",
+                                      port_id, strerror(-ret));
        }
 }
 
-- 
2.53.0

Reply via email to