diff --git a/lib/librte_ethdev/rte_ethdev.c b/lib/librte_ethdev/rte_ethdev.c index abcc31c19e..76699cc10f 100644 --- a/lib/librte_ethdev/rte_ethdev.c +++ b/lib/librte_ethdev/rte_ethdev.c @@ -2862,7 +2862,8 @@ rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats, int ret; RTE_ETH_VALID_PORTID_OR_ERR_RET(port_id, -EINVAL); - + if (xstats == NULL && n > 0) + return -EINVAL; dev = &rte_eth_devices[port_id]; nb_rxqs = RTE_MIN(dev->data->nb_rx_queues, RTE_ETHDEV_QUEUE_STAT_CNTRS); @@ -2878,7 +2879,7 @@ rte_eth_xstats_get(uint16_t port_id, struct rte_eth_xstat *xstats, * xstats struct. */ xcount = (*dev->dev_ops->xstats_get)(dev, - xstats ? xstats + count : NULL, + (n > count) ? xstats + count : NULL, (n > count) ? n - count : 0); if (xcount < 0) diff --git a/lib/librte_ethdev/rte_ethdev.h b/lib/librte_ethdev/rte_ethdev.h index 9cc12ef97e..826339009b 100644 --- a/lib/librte_ethdev/rte_ethdev.h +++ b/lib/librte_ethdev/rte_ethdev.h @@ -2363,9 +2363,13 @@ int rte_eth_xstats_get_names(uint16_t port_id, * @param xstats * A pointer to a table of structure of type *rte_eth_xstat* * to be filled with device statistics ids and values. - * This parameter can be set to NULL if n is 0. + * This parameter can be set to NULL if and only if n is 0. * @param n * The size of the xstats array (number of elements). + * If lower than the required number of elements, the function returns + * the required number of elements. + * If equal to zero, the xstats must be NULL, the function returns the + * required number of elements. * @return * - A positive value lower or equal to n: success. The return value * is the number of entries filled in the stats table.