DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH v6 3/9] test: avoid overflowing huge directory path
Date: Sun, 23 Nov 2025 09:43:26 -0800	[thread overview]
Message-ID: <20251123174503.20880-4-stephen@networkplumber.org> (raw)
In-Reply-To: <20251123174503.20880-1-stephen@networkplumber.org>

Modify the part of the test that is scanning for hugepages
mount point to use existing getmntent library calls.

As a result, the maximum mount path is smaller which is ok
since this is all handled by the standard library.
And the resulting huge path arguments don't overflow.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_eal_flags.c | 115 ++++++++++++++------------------------
 1 file changed, 43 insertions(+), 72 deletions(-)

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index c2e6c00edb..0aebb5f611 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -107,6 +107,7 @@ test_misc_flags(void)
 #include <sys/wait.h>
 #include <limits.h>
 #include <fcntl.h>
+#include <mntent.h>
 
 #include <rte_lcore.h>
 #include <rte_debug.h>
@@ -123,6 +124,8 @@ test_misc_flags(void)
 #define vdev "--vdev"
 #define file_prefix "--file-prefix"
 
+#define FS_HUGETLB "hugetlbfs"
+
 #define memtest "memtest"
 #define memtest1 "memtest1"
 #define memtest2 "memtest2"
@@ -136,24 +139,6 @@ enum hugepage_action {
 	HUGEPAGE_INVALID
 };
 
-/* if string contains a hugepage path */
-static int
-get_hugepage_path(char * src, int src_len, char * dst, int dst_len)
-{
-#define NUM_TOKENS 4
-	char *tokens[NUM_TOKENS];
-
-	/* if we couldn't properly split the string */
-	if (rte_strsplit(src, src_len, tokens, NUM_TOKENS, ' ') < NUM_TOKENS)
-		return 0;
-
-	if (strncmp(tokens[2], "hugetlbfs", sizeof("hugetlbfs")) == 0) {
-		strlcpy(dst, tokens[1], dst_len);
-		return 1;
-	}
-	return 0;
-}
-
 /*
  * Cycles through hugepage directories and looks for hugepage
  * files associated with a given prefix. Depending on value of
@@ -165,45 +150,39 @@ get_hugepage_path(char * src, int src_len, char * dst, int dst_len)
  * Returns -1 if it encounters an error
  */
 static int
-process_hugefiles(const char * prefix, enum hugepage_action action)
+process_hugefiles(const char *prefix, enum hugepage_action action)
 {
-	FILE * hugedir_handle = NULL;
-	DIR * hugepage_dir = NULL;
-	struct dirent *dirent = NULL;
-
-	char hugefile_prefix[PATH_MAX] = {0};
-	char hugedir[PATH_MAX] = {0};
-	char line[PATH_MAX] = {0};
-
-	int fd, lck_result, result = 0;
+	const struct mntent *entry;
+	char hugefile_prefix[PATH_MAX];
+	int result = 0;
 
-	const int prefix_len = snprintf(hugefile_prefix,
-			sizeof(hugefile_prefix), "%smap_", prefix);
-	if (prefix_len <= 0 || prefix_len >= (int)sizeof(hugefile_prefix)
-			|| prefix_len >= (int)sizeof(dirent->d_name)) {
+	const int prefix_len = snprintf(hugefile_prefix, sizeof(hugefile_prefix), "%smap_", prefix);
+	if (prefix_len <= 0 || prefix_len >= NAME_MAX) {
 		printf("Error creating hugefile filename prefix\n");
 		return -1;
 	}
 
 	/* get hugetlbfs mountpoints from /proc/mounts */
-	hugedir_handle = fopen("/proc/mounts", "r");
-
-	if (hugedir_handle == NULL) {
+	FILE *mounts = setmntent("/proc/mounts", "r");
+	if (mounts == NULL) {
 		printf("Error parsing /proc/mounts!\n");
 		return -1;
 	}
 
-	/* read and parse script output */
-	while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
+	/* foreach mountpoint */
+	while ((entry = getmntent(mounts)) != NULL) {
+		DIR *hugepage_dir;
+		struct dirent *dirent;
 
-		/* check if we have a hugepage filesystem path */
-		if (!get_hugepage_path(line, sizeof(line), hugedir, sizeof(hugedir)))
+		/* only want hugetlbfs filesystems */
+		if (strcmp(entry->mnt_type, FS_HUGETLB) != 0)
 			continue;
 
 		/* check if directory exists */
-		if ((hugepage_dir = opendir(hugedir)) == NULL) {
-			fclose(hugedir_handle);
-			printf("Error reading %s: %s\n", hugedir, strerror(errno));
+		hugepage_dir = opendir(entry->mnt_dir);
+		if (hugepage_dir == NULL) {
+			endmntent(mounts);
+			printf("Error reading %s: %s\n", entry->mnt_dir, strerror(errno));
 			return -1;
 		}
 
@@ -222,10 +201,10 @@ process_hugefiles(const char * prefix, enum hugepage_action action)
 				break;
 			case HUGEPAGE_DELETE:
 				{
-					char file_path[PATH_MAX] = {0};
+					char file_path[PATH_MAX];
 
 					snprintf(file_path, sizeof(file_path),
-						"%s/%s", hugedir, dirent->d_name);
+						"%s/%s", entry->mnt_dir, dirent->d_name);
 
 					/* remove file */
 					if (remove(file_path) < 0) {
@@ -240,6 +219,8 @@ process_hugefiles(const char * prefix, enum hugepage_action action)
 				break;
 			case HUGEPAGE_CHECK_LOCKED:
 				{
+					int fd;
+
 					/* try and lock the file */
 					fd = openat(dirfd(hugepage_dir), dirent->d_name, O_RDONLY);
 
@@ -253,10 +234,7 @@ process_hugefiles(const char * prefix, enum hugepage_action action)
 					}
 
 					/* non-blocking lock */
-					lck_result = flock(fd, LOCK_EX | LOCK_NB);
-
-					/* if lock succeeds, there's something wrong */
-					if (lck_result != -1) {
+					if (flock(fd, LOCK_EX | LOCK_NB) != -1) {
 						result = 0;
 
 						/* unlock the resulting lock */
@@ -278,7 +256,7 @@ process_hugefiles(const char * prefix, enum hugepage_action action)
 		closedir(hugepage_dir);
 	} /* read /proc/mounts */
 end:
-	fclose(hugedir_handle);
+	endmntent(mounts);
 	return result;
 }
 
@@ -873,10 +851,10 @@ test_no_huge_flag(void)
 static int
 test_misc_flags(void)
 {
-	char hugepath[PATH_MAX] = {0};
-	char hugepath_dir[PATH_MAX] = {0};
-	char hugepath_dir2[PATH_MAX] = {0};
-	char hugepath_dir3[PATH_MAX] = {0};
+	const char *hugepath = "";
+	char hugepath_dir[PATH_MAX];
+	char hugepath_dir2[PATH_MAX];
+	char hugepath_dir3[PATH_MAX];
 #ifdef RTE_EXEC_ENV_FREEBSD
 	/* BSD target doesn't support prefixes at this point */
 	const char * prefix = "";
@@ -884,9 +862,7 @@ test_misc_flags(void)
 #else
 	const char *prefix = get_file_prefix();
 	const char * nosh_prefix = "--file-prefix=noshconf";
-	FILE * hugedir_handle = NULL;
-	char line[PATH_MAX] = {0};
-	unsigned i, isempty = 1;
+	struct mntent *entry;
 
 	if (prefix == NULL)
 		return -1;
@@ -896,29 +872,24 @@ test_misc_flags(void)
 	 */
 
 	/* get hugetlbfs mountpoints from /proc/mounts */
-	hugedir_handle = fopen("/proc/mounts", "r");
-
-	if (hugedir_handle == NULL) {
+	FILE *mounts = setmntent("/proc/mounts", "r");
+	if (mounts == NULL) {
 		printf("Error opening /proc/mounts!\n");
 		return -1;
 	}
 
-	/* read /proc/mounts */
-	while (fgets(line, sizeof(line), hugedir_handle) != NULL) {
-
-		/* find first valid hugepath */
-		if (get_hugepage_path(line, sizeof(line), hugepath, sizeof(hugepath)))
+	/* foreach mount point */
+	hugepath = NULL;
+	while ((entry = getmntent(mounts)) != NULL) {
+		/* only want hugetlbfs filesystems */
+		if (strcmp(entry->mnt_type, FS_HUGETLB) == 0) {
+			hugepath = strdupa(entry->mnt_dir);
 			break;
+		}
 	}
+	endmntent(mounts);
 
-	fclose(hugedir_handle);
-
-	/* check if path is not empty */
-	for (i = 0; i < sizeof(hugepath); i++)
-		if (hugepath[i] != '\0')
-			isempty = 0;
-
-	if (isempty) {
+	if (hugepath == NULL) {
 		printf("No mounted hugepage dir found!\n");
 		return -1;
 	}
-- 
2.51.0


  parent reply	other threads:[~2025-11-23 17:45 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-10 18:19 [PATCH 0/3] examples: format truncation bugs Stephen Hemminger
2025-11-10 18:19 ` [PATCH 1/3] examples/server_node_efd: fix format overflow Stephen Hemminger
2025-11-10 18:19 ` [PATCH 2/3] examples/vdpa: fix format overflow warning Stephen Hemminger
2025-11-10 18:19 ` [PATCH 3/3] examples: re-enable format truncation warning Stephen Hemminger
2025-11-11 22:17 ` [PATCH v2 0/7] fix format-truncation warnings Stephen Hemminger
2025-11-11 22:17   ` [PATCH v2 1/7] examples/server_node_efd: fix format overflow Stephen Hemminger
2025-11-11 22:17   ` [PATCH v2 2/7] examples/vdpa: fix format overflow warning Stephen Hemminger
2025-11-14 15:59     ` Bruce Richardson
2025-11-11 22:17   ` [PATCH v2 3/7] examples/ip_reassembly: add check before formatting name Stephen Hemminger
2025-11-12 14:50     ` Konstantin Ananyev
2025-11-14 16:05     ` Bruce Richardson
2025-11-11 22:17   ` [PATCH v2 4/7] examples: re-enable format truncation warning Stephen Hemminger
2025-11-14 16:07     ` Bruce Richardson
2025-11-11 22:17   ` [PATCH v2 5/7] test: refactor file prefix arg handling Stephen Hemminger
2025-11-11 22:17   ` [PATCH v2 6/7] test: increase size of memzone name Stephen Hemminger
2025-11-14 16:07     ` Bruce Richardson
2025-11-11 22:17   ` [PATCH v2 7/7] test: re-enable format-truncation warnings Stephen Hemminger
2025-11-14 16:08     ` Bruce Richardson
2025-11-15 19:36 ` [PATCH v3 0/4] examples: fix " Stephen Hemminger
2025-11-15 19:36   ` [PATCH v3 1/4] examples/server_node_efd: fix format overflow Stephen Hemminger
2025-11-17  8:50     ` Bruce Richardson
2025-11-15 19:36   ` [PATCH v3 2/4] examples/vdpa: fix format overflow warning Stephen Hemminger
2025-11-19  4:31     ` Thomas Monjalon
2025-11-19  9:13       ` Bruce Richardson
2025-11-15 19:36   ` [PATCH v3 3/4] examples/ip_reassembly: add check before formatting name Stephen Hemminger
2025-11-15 19:36   ` [PATCH v3 4/4] examples: enable format truncation warning Stephen Hemminger
2025-11-20 16:21 ` [PATCH v4 0/4] examples: fix format-truncation errors Stephen Hemminger
2025-11-20 16:21   ` [PATCH v4 1/4] examples/server_node_efd: fix format overflow Stephen Hemminger
2025-11-20 16:22   ` [PATCH v4 2/4] examples/vdpa: fix format overflow warning Stephen Hemminger
2025-11-20 16:22   ` [PATCH v4 3/4] examples/ip_reassembly: add check before formatting name Stephen Hemminger
2025-11-20 16:22   ` [PATCH v4 4/4] examples: enable format truncation warning Stephen Hemminger
2025-11-21 17:08 ` [PATCH v5 0/8] tests: fix format truncation warnings Stephen Hemminger
2025-11-21 17:08   ` [PATCH v5 1/8] test: increase size of memzone name Stephen Hemminger
2025-11-21 17:08   ` [PATCH v5 2/8] test: refactor file prefix arg handling Stephen Hemminger
2025-11-21 17:08   ` [PATCH v5 3/8] test: avoid overflowing huge directory path Stephen Hemminger
2025-11-21 17:08   ` [PATCH v5 4/8] test: use unit test runner for eal flags Stephen Hemminger
2025-11-21 17:08   ` [PATCH v5 5/8] test: fix format overflow warning in ACL test Stephen Hemminger
2025-11-21 17:08   ` [PATCH v5 6/8] test: fix impossible format-truncation in cfgfiles Stephen Hemminger
2025-11-21 17:08   ` [PATCH v5 7/8] test: fix format overflow in cryptodev test Stephen Hemminger
2025-11-21 17:08   ` [PATCH v5 8/8] test: re-enable format-truncation warnings Stephen Hemminger
2025-11-23 17:43 ` [PATCH v6 0/9] tests: fix format truncation warnings Stephen Hemminger
2025-11-23 17:43   ` [PATCH v6 1/9] test: increase size of memzone name Stephen Hemminger
2025-11-23 17:43   ` [PATCH v6 2/9] test: refactor file prefix arg handling Stephen Hemminger
2025-11-23 17:43   ` Stephen Hemminger [this message]
2025-11-23 17:43   ` [PATCH v6 4/9] test: use unit test runner for eal flags Stephen Hemminger
2025-11-23 17:43   ` [PATCH v6 5/9] test: fix format overflow warning in ACL test Stephen Hemminger
2025-11-23 17:43   ` [PATCH v6 6/9] test: fix impossible format-truncation in cfgfiles Stephen Hemminger
2025-11-23 17:43   ` [PATCH v6 7/9] test: fix format overflow in cryptodev test Stephen Hemminger
2025-11-23 17:43   ` [PATCH v6 8/9] test: fix overflow warnings in mp_secondary and pdump Stephen Hemminger
2025-11-23 17:43   ` [PATCH v6 9/9] test: re-enable format-truncation warnings Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251123174503.20880-4-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).