When constructing hugepage path, allocate the variable
to avoid a possible overflow which was detected in debug build:

In function 'find_numasocket':
lib/eal/linux/eal_memory.c:450:31:
        error: 'snprintf' output may be truncated
        before the last format character [-Werror=format-truncation=]
lib/eal/linux/eal_memory.c:449:9:
        note: 'snprintf' output 2 or more bytes (assuming 4097)
        into a destination of size 4096
  449 | snprintf(hugedir_str, sizeof(hugedir_str),
  450 |                 "%s/%s", hpi->hugedir, eal_get_hugefile_prefix());

This is a new warning (format-truncation) enabled in DPDK 26.03.

Bugzilla ID: 1878

Signed-off-by: Thomas Monjalon <[email protected]>
---
 lib/eal/linux/eal_memory.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/lib/eal/linux/eal_memory.c b/lib/eal/linux/eal_memory.c
index 7c4197a5b8..ceda9c3246 100644
--- a/lib/eal/linux/eal_memory.c
+++ b/lib/eal/linux/eal_memory.c
@@ -436,8 +436,9 @@ find_numasocket(struct hugepage_file *hugepg_tbl, struct 
hugepage_info *hpi)
        unsigned i, hp_count = 0;
        uint64_t virt_addr;
        char buf[BUFSIZ];
-       char hugedir_str[PATH_MAX];
+       char *hugedir_str;
        FILE *f;
+       int ret;
 
        f = fopen("/proc/self/numa_maps", "r");
        if (f == NULL) {
@@ -446,8 +447,14 @@ find_numasocket(struct hugepage_file *hugepg_tbl, struct 
hugepage_info *hpi)
                return 0;
        }
 
-       snprintf(hugedir_str, sizeof(hugedir_str),
-                       "%s/%s", hpi->hugedir, eal_get_hugefile_prefix());
+       ret = asprintf(&hugedir_str, "%s/%s",
+                       hpi->hugedir, eal_get_hugefile_prefix());
+       if (ret < 0) {
+               EAL_LOG(ERR, "%s(): failed to store hugepage path", __func__);
+               goto error;
+       }
+
+       ret = -1;
 
        /* parse numa map */
        while (fgets(buf, sizeof(buf), f) != NULL) {
@@ -503,12 +510,11 @@ find_numasocket(struct hugepage_file *hugepg_tbl, struct 
hugepage_info *hpi)
        if (hp_count < hpi->num_pages[0])
                goto error;
 
-       fclose(f);
-       return 0;
-
+       ret = 0;
 error:
+       free(hugedir_str);
        fclose(f);
-       return -1;
+       return ret;
 }
 
 static int
-- 
2.52.0

Reply via email to