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>,
	Tyler Retzlaff <roretzla@linux.microsoft.com>
Subject: [PATCH v2 5/7] test: refactor file prefix arg handling
Date: Tue, 11 Nov 2025 14:17:22 -0800	[thread overview]
Message-ID: <20251111221857.443752-6-stephen@networkplumber.org> (raw)
In-Reply-To: <20251111221857.443752-1-stephen@networkplumber.org>

Make setting up --file-prefix arg logic into a function,
and avoid impossible case of file prefix path causing overflow
of string.

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

diff --git a/app/test/test_eal_flags.c b/app/test/test_eal_flags.c
index e32f83d3c8..05b4135a51 100644
--- a/app/test/test_eal_flags.c
+++ b/app/test/test_eal_flags.c
@@ -121,6 +121,8 @@ test_misc_flags(void)
 #define no_shconf "--no-shconf"
 #define allow "--allow"
 #define vdev "--vdev"
+#define file_prefix "--file-prefix"
+
 #define memtest "memtest"
 #define memtest1 "memtest1"
 #define memtest2 "memtest2"
@@ -312,26 +314,40 @@ get_number_of_sockets(void)
 }
 #endif
 
-/*
- * Test that the app doesn't run with invalid allow option.
- * Final tests ensures it does run with valid options as sanity check (one
- * test for with Domain+BDF, second for just with BDF)
- */
+/* Get the file_prefix xommand line argument */
 static int
-test_allow_flag(void)
+get_file_prefix(char *prefix, size_t size)
 {
-	unsigned i;
 #ifdef RTE_EXEC_ENV_FREEBSD
 	/* BSD target doesn't support prefixes at this point */
-	const char * prefix = "";
+	RTE_SET_USED(len);
+	*prefix = 0;
+	return 0;
 #else
-	char prefix[PATH_MAX], tmp[PATH_MAX];
+	char tmp[PATH_MAX];
+
 	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
 		printf("Error - unable to get current prefix!\n");
 		return -1;
 	}
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
+	snprintf(prefix, size, "%s=%s", file_prefix, tmp);
+	return 0;
 #endif
+}
+
+/*
+ * Test that the app doesn't run with invalid allow option.
+ * Final tests ensures it does run with valid options as sanity check (one
+ * test for with Domain+BDF, second for just with BDF)
+ */
+static int
+test_allow_flag(void)
+{
+	unsigned i;
+	char prefix[PATH_MAX + sizeof(file_prefix)];
+
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	const char *wlinval[][7] = {
 		{prgname, prefix, mp_flag,
@@ -387,17 +403,10 @@ test_allow_flag(void)
 static int
 test_invalid_b_flag(void)
 {
-#ifdef RTE_EXEC_ENV_FREEBSD
-	/* BSD target doesn't support prefixes at this point */
-	const char * prefix = "";
-#else
-	char prefix[PATH_MAX], tmp[PATH_MAX];
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		printf("Error - unable to get current prefix!\n");
-		return -1;
-	}
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif
+	char prefix[PATH_MAX + sizeof(file_prefix)];
+
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	const char *blinval[][5] = {
 		{prgname, prefix, mp_flag, "-b", "error"},
@@ -491,17 +500,10 @@ test_invalid_vdev_flag(void)
 static int
 test_invalid_r_flag(void)
 {
-#ifdef RTE_EXEC_ENV_FREEBSD
-	/* BSD target doesn't support prefixes at this point */
-	const char * prefix = "";
-#else
-	char prefix[PATH_MAX], tmp[PATH_MAX];
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		printf("Error - unable to get current prefix!\n");
-		return -1;
-	}
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif
+	char prefix[PATH_MAX + sizeof(file_prefix)];
+
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	const char *rinval[][5] = {
 			{prgname, prefix, mp_flag, "-r", "error"},
@@ -535,17 +537,10 @@ test_invalid_r_flag(void)
 static int
 test_missing_c_flag(void)
 {
-#ifdef RTE_EXEC_ENV_FREEBSD
-	/* BSD target doesn't support prefixes at this point */
-	const char * prefix = "";
-#else
-	char prefix[PATH_MAX], tmp[PATH_MAX];
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		printf("Error - unable to get current prefix!\n");
-		return -1;
-	}
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif
+	char prefix[PATH_MAX + sizeof(file_prefix)];
+
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	/* -c flag but no coremask value */
 	const char *argv1[] = { prgname, prefix, mp_flag, "-c"};
@@ -694,17 +689,10 @@ test_missing_c_flag(void)
 static int
 test_main_lcore_flag(void)
 {
-#ifdef RTE_EXEC_ENV_FREEBSD
-	/* BSD target doesn't support prefixes at this point */
-	const char *prefix = "";
-#else
-	char prefix[PATH_MAX], tmp[PATH_MAX];
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		printf("Error - unable to get current prefix!\n");
-		return -1;
-	}
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif
+	char prefix[PATH_MAX + sizeof(file_prefix)];
+
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	if (!rte_lcore_is_enabled(0) || !rte_lcore_is_enabled(1))
 		return TEST_SKIPPED;
@@ -751,17 +739,10 @@ test_main_lcore_flag(void)
 static int
 test_invalid_n_flag(void)
 {
-#ifdef RTE_EXEC_ENV_FREEBSD
-	/* BSD target doesn't support prefixes at this point */
-	const char * prefix = "";
-#else
-	char prefix[PATH_MAX], tmp[PATH_MAX];
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		printf("Error - unable to get current prefix!\n");
-		return -1;
-	}
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif
+	char prefix[PATH_MAX + sizeof(file_prefix)];
+
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	/* -n flag but no value */
 	const char *argv1[] = { prgname, prefix, no_huge, no_shconf,
@@ -803,18 +784,10 @@ test_invalid_n_flag(void)
 static int
 test_no_hpet_flag(void)
 {
-	char prefix[PATH_MAX] = "";
+	char prefix[PATH_MAX + sizeof(file_prefix)];
 
-#ifdef RTE_EXEC_ENV_FREEBSD
-	return 0;
-#else
-	char tmp[PATH_MAX];
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		printf("Error - unable to get current prefix!\n");
-		return -1;
-	}
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	/* With --no-hpet */
 	const char *argv1[] = {prgname, prefix, mp_flag, no_hpet};
@@ -913,17 +886,14 @@ test_misc_flags(void)
 	const char * prefix = "";
 	const char * nosh_prefix = "";
 #else
-	char prefix[PATH_MAX], tmp[PATH_MAX];
+	char prefix[PATH_MAX + sizeof(file_prefix)];
 	const char * nosh_prefix = "--file-prefix=noshconf";
 	FILE * hugedir_handle = NULL;
 	char line[PATH_MAX] = {0};
 	unsigned i, isempty = 1;
 
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		printf("Error - unable to get current prefix!\n");
-		return -1;
-	}
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	/*
 	 * get first valid hugepage path
@@ -1239,16 +1209,10 @@ test_file_prefix(void)
 	 *    run in legacy mode, and not present at all after run in default
 	 *    mem mode
 	 */
-	char prefix[PATH_MAX] = "";
+	char prefix[PATH_MAX + sizeof(file_prefix)];
 
-#ifdef RTE_EXEC_ENV_FREEBSD
-	return 0;
-#else
-	if (get_current_prefix(prefix, sizeof(prefix)) == NULL) {
-		printf("Error - unable to get current prefix!\n");
-		return -1;
-	}
-#endif
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	/* this should fail unless the test itself is run with "memtest" prefix */
 	const char *argv0[] = {prgname, mp_flag, "-m",
@@ -1525,17 +1489,10 @@ populate_socket_mem_param(int num_sockets, const char *mem,
 static int
 test_memory_flags(void)
 {
-#ifdef RTE_EXEC_ENV_FREEBSD
-	/* BSD target doesn't support prefixes at this point */
-	const char * prefix = "";
-#else
-	char prefix[PATH_MAX], tmp[PATH_MAX];
-	if (get_current_prefix(tmp, sizeof(tmp)) == NULL) {
-		printf("Error - unable to get current prefix!\n");
-		return -1;
-	}
-	snprintf(prefix, sizeof(prefix), "--file-prefix=%s", tmp);
-#endif
+	char prefix[PATH_MAX + sizeof(file_prefix)];
+
+	if (get_file_prefix(prefix, sizeof(prefix)) != 0)
+		return TEST_FAILED;
 
 	/* valid -m flag and mp flag */
 	const char *argv0[] = {prgname, prefix, mp_flag,
-- 
2.51.0


  parent reply	other threads:[~2025-11-11 22:19 UTC|newest]

Thread overview: 12+ 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-11 22:17   ` [PATCH v2 3/7] examples/ip_reassembly: add check before formatting name Stephen Hemminger
2025-11-11 22:17   ` [PATCH v2 4/7] examples: re-enable format truncation warning Stephen Hemminger
2025-11-11 22:17   ` Stephen Hemminger [this message]
2025-11-11 22:17   ` [PATCH v2 6/7] test: increase size of memzone name Stephen Hemminger
2025-11-11 22:17   ` [PATCH v2 7/7] 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=20251111221857.443752-6-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    --cc=roretzla@linux.microsoft.com \
    /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).