[PATCH v13 14/17] ethdev: avoid possible overflow in xstat names

Stephen Hemminger
2026-01-27T16:34:42Z

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 <stephen@networkplumber.org>
Acked-by: Bruce Richardson <bruce.richardson@intel.com>
---
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 2659e8d9eb..8026e7adb4 100644
--- a/lib/ethdev/rte_ethdev.c
+++ b/lib/ethdev/rte_ethdev.c
@@ -3488,9 +3488,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++;
}

@@ -3500,10 +3501,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++;
}

@@ -3511,10 +3519,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