Bug #8273
opendpdk: adjust variable sizes in xstats printing
Description
Address copilot comments from https://github.com/OISF/suricata/pull/14771
src/source-dpdk.c
Comment on lines +493 to +496
uint16_t len = (uint16_t)ret;
xstats = SCCalloc(len, sizeof(*xstats));
if (xstats == NULL) {
Copilot AI
yesterday
len is stored as uint16_t, but rte_eth_xstats_get(..., NULL, 0) returns an int count and the API takes an unsigned int n. If the xstat count exceeds 65535, this will truncate len, leading to under-allocation and subsequent ret > len paths. Use an unsigned int/uint32_t for len (and the allocation size) to match the DPDK API and avoid truncation.
src/source-dpdk.c
Comment on lines +488 to +499
int32_t ret = rte_eth_xstats_get(port_id, NULL, 0);
if (ret < 0) {
SCLogPerf("%s: unable to obtain rte_eth_xstats (%s)", port_name, rte_strerror(-ret));
return;
}
uint16_t len = (uint16_t)ret;
xstats = SCCalloc(len, sizeof(*xstats));
if (xstats NULL) {
SCLogWarning("Failed to allocate memory for the rte_eth_xstat structure");
return;
}
Copilot AI
yesterday
If rte_eth_xstats_get(port_id, NULL, 0) returns 0 (no xstats), SCCalloc(len, ...) will be called with len 0, which may return NULL and triggers a misleading "Failed to allocate" warning. Consider handling len == 0 as a valid case and return early without logging a warning.
src/source-dpdk.c
Comment on lines +501 to +505
ret = rte_eth_xstats_get(port_id, xstats, len);
if (ret < 0 || ret > len) {
SCFree(xstats);
SCLogPerf("%s: unable to obtain rte_eth_xstats (%s)", port_name, rte_strerror(-ret));
return;
Copilot AI
yesterday
In the ret < 0 || ret > len branch, the log uses rte_strerror(-ret). When ret > len, ret is positive (not an errno), so passing -ret to rte_strerror is incorrect/misleading. Split the cases: use rte_strerror(-ret) only for ret < 0, and for ret > len log an "unexpected xstats count" style message (and consider re-querying the required size).