The compiler doesn't know that all the elements in the table of queue stats are short enough to avoid overflowing the snprintf. Add a condition to warn if it ever does; maybe some day a new long named queue stat could be added.
Signed-off-by: Stephen Hemminger <[email protected]> Acked-by: Bruce Richardson <[email protected]> --- lib/ethdev/rte_ethdev.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/ethdev/rte_ethdev.c b/lib/ethdev/rte_ethdev.c index c6fe0d5165..05de3335e5 100644 --- a/lib/ethdev/rte_ethdev.c +++ b/lib/ethdev/rte_ethdev.c @@ -3489,9 +3489,10 @@ eth_basic_stats_get_names(struct rte_eth_dev *dev, uint16_t num_q; for (idx = 0; idx < RTE_NB_STATS; idx++) { - strlcpy(xstats_names[cnt_used_entries].name, - eth_dev_stats_strings[idx].name, - sizeof(xstats_names[0].name)); + if (strlcpy(xstats_names[cnt_used_entries].name, eth_dev_stats_strings[idx].name, + sizeof(xstats_names[0].name)) >= sizeof(xstats_names[0].name)) + RTE_ETHDEV_LOG_LINE(ERR, "statistic name '%s' will be truncated", + xstats_names[cnt_used_entries].name); cnt_used_entries++; } @@ -3501,10 +3502,17 @@ eth_basic_stats_get_names(struct rte_eth_dev *dev, num_q = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS); for (id_queue = 0; id_queue < num_q; id_queue++) { for (idx = 0; idx < RTE_NB_RXQ_STATS; idx++) { - snprintf(xstats_names[cnt_used_entries].name, - sizeof(xstats_names[0].name), - "rx_q%u_%s", - id_queue, eth_dev_rxq_stats_strings[idx].name); + unsigned int cc; + + cc = snprintf(xstats_names[cnt_used_entries].name, + sizeof(xstats_names[0].name), + "rx_q%u_%s", + id_queue, eth_dev_rxq_stats_strings[idx].name); + + /* could only happen if a long string was added */ + if (cc >= sizeof(xstats_names[0].name)) + RTE_ETHDEV_LOG_LINE(ERR, "truncated rxq stat string '%s'", + eth_dev_rxq_stats_strings[idx].name); cnt_used_entries++; } @@ -3512,10 +3520,15 @@ eth_basic_stats_get_names(struct rte_eth_dev *dev, num_q = RTE_MIN(dev->data->nb_tx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS); for (id_queue = 0; id_queue < num_q; id_queue++) { for (idx = 0; idx < RTE_NB_TXQ_STATS; idx++) { - snprintf(xstats_names[cnt_used_entries].name, - sizeof(xstats_names[0].name), - "tx_q%u_%s", - id_queue, eth_dev_txq_stats_strings[idx].name); + unsigned int cc; + + cc = snprintf(xstats_names[cnt_used_entries].name, + sizeof(xstats_names[0].name), + "tx_q%u_%s", + id_queue, eth_dev_txq_stats_strings[idx].name); + if (cc >= sizeof(xstats_names[0].name)) + RTE_ETHDEV_LOG_LINE(ERR, "truncated txq stat string '%s'", + eth_dev_txq_stats_strings[idx].name); cnt_used_entries++; } } -- 2.51.0

