The three resource helpers and the UIO name lookup each built a path with an unchecked sprintf() into a 64 byte buffer and then read the value with fscanf(). Use the EAL routines instead; base 0 conversion handles the "0x" prefix these files use.
Signed-off-by: Stephen Hemminger <[email protected]> --- drivers/common/ionic/ionic_common_uio.c | 63 +++++-------------------- 1 file changed, 12 insertions(+), 51 deletions(-) diff --git a/drivers/common/ionic/ionic_common_uio.c b/drivers/common/ionic/ionic_common_uio.c index aaefab918c..a796281bdd 100644 --- a/drivers/common/ionic/ionic_common_uio.c +++ b/drivers/common/ionic/ionic_common_uio.c @@ -19,6 +19,7 @@ #include <rte_common.h> #include <rte_eal.h> #include <rte_string_fns.h> +#include <rte_sysfs.h> #include "ionic_common.h" @@ -63,30 +64,17 @@ struct uio_name { static void uio_fill_name_cache(struct uio_name *name_cache, const char *pfx) { - char file[64]; - FILE *fp; - char *ret; int name_idx = 0; int i; for (i = 0; i < IONIC_UIO_MAX_TRIES && name_idx < IONIC_MAX_DEVICES; i++) { - sprintf(file, "/sys/class/uio/uio%d/name", i); - - fp = fopen(file, "r"); - if (fp == NULL) - continue; - - ret = fgets(name_cache[name_idx].name, IONIC_MAX_NAME_LEN, fp); - if (ret == NULL) { - fclose(fp); + if (rte_sysfs_parse_string(name_cache[name_idx].name, IONIC_MAX_NAME_LEN, + "/sys/class/uio/uio%d/name", i) < 0) continue; - } name_cache[name_idx].idx = i; - fclose(fp); - if (strncmp(name_cache[name_idx].name, pfx, strlen(pfx)) == 0) name_idx++; } @@ -215,21 +203,12 @@ static unsigned long uio_get_res_size(int uio_idx, int res_idx) { unsigned long size; - char file[64]; - FILE *fp; - - sprintf(file, "/sys/class/uio/uio%d/maps/map%d/size", - uio_idx, res_idx); - fp = fopen(file, "r"); - if (fp == NULL) + /* zero is the error value for all of these */ + if (rte_sysfs_parse_uint(&size, "/sys/class/uio/uio%d/maps/map%d/size", + uio_idx, res_idx) < 0) return 0; - if (fscanf(fp, "0x%lx", &size) != 1) - size = 0; - - fclose(fp); - return size; } @@ -237,21 +216,12 @@ static unsigned long uio_get_res_phy_addr_offs(int uio_idx, int res_idx) { unsigned long offset; - char file[64]; - FILE *fp; - - sprintf(file, "/sys/class/uio/uio%d/maps/map%d/offset", - uio_idx, res_idx); - fp = fopen(file, "r"); - if (fp == NULL) + /* zero is the error value for all of these */ + if (rte_sysfs_parse_uint(&offset, "/sys/class/uio/uio%d/maps/map%d/offset", + uio_idx, res_idx) < 0) return 0; - if (fscanf(fp, "0x%lx", &offset) != 1) - offset = 0; - - fclose(fp); - return offset; } @@ -259,21 +229,12 @@ static unsigned long uio_get_res_phy_addr(int uio_idx, int res_idx) { unsigned long addr; - char file[64]; - FILE *fp; - sprintf(file, "/sys/class/uio/uio%d/maps/map%d/addr", - uio_idx, res_idx); - - fp = fopen(file, "r"); - if (fp == NULL) + /* zero is the error value for all of these */ + if (rte_sysfs_parse_uint(&addr, "/sys/class/uio/uio%d/maps/map%d/addr", + uio_idx, res_idx) < 0) return 0; - if (fscanf(fp, "0x%lx", &addr) != 1) - addr = 0; - - fclose(fp); - return addr; } -- 2.53.0

