DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH 1/2] eal: add strsep() for Windows builds
@ 2025-05-16 13:22 Andre Muezerie
  2025-05-16 13:22 ` [PATCH 2/2] test/strsep: add tests for function strsep() Andre Muezerie
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Andre Muezerie @ 2025-05-16 13:22 UTC (permalink / raw)
  To: Dmitry Kozlyuk, Tyler Retzlaff; +Cc: dev, Andre Muezerie

Function strsep() is used by DPDK code but is not available on Windows.

This patch adds an implementation from NetBSD, unblocking code depending
on it from being compiled on Windows.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 lib/eal/windows/include/rte_os_shim.h |  1 +
 lib/eal/windows/include/strsep.h      |  9 +++++
 lib/eal/windows/meson.build           |  1 +
 lib/eal/windows/strsep.c              | 47 +++++++++++++++++++++++++++
 4 files changed, 58 insertions(+)
 create mode 100644 lib/eal/windows/include/strsep.h
 create mode 100644 lib/eal/windows/strsep.c

diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
index 0e74eb19c7..9c3da554da 100644
--- a/lib/eal/windows/include/rte_os_shim.h
+++ b/lib/eal/windows/include/rte_os_shim.h
@@ -7,6 +7,7 @@
 
 #include <rte_os.h>
 #include <rte_windows.h>
+#include <strsep.h>
 
 /**
  * @file
diff --git a/lib/eal/windows/include/strsep.h b/lib/eal/windows/include/strsep.h
new file mode 100644
index 0000000000..d5d2860d48
--- /dev/null
+++ b/lib/eal/windows/include/strsep.h
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California.  All rights reserved.
+ */
+
+#pragma once
+
+char *
+strsep(char **stringp, const char *delim);
diff --git a/lib/eal/windows/meson.build b/lib/eal/windows/meson.build
index 7756d417be..eadecd473c 100644
--- a/lib/eal/windows/meson.build
+++ b/lib/eal/windows/meson.build
@@ -19,6 +19,7 @@ sources += files(
         'eal_timer.c',
         'getopt.c',
         'rte_thread.c',
+        'strsep.c',
 )
 
 dpdk_conf.set10('RTE_EAL_NUMA_AWARE_HUGEPAGES', true)
diff --git a/lib/eal/windows/strsep.c b/lib/eal/windows/strsep.c
new file mode 100644
index 0000000000..fdc0688a07
--- /dev/null
+++ b/lib/eal/windows/strsep.c
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California.  All rights reserved.
+ */
+
+#include <string.h>
+
+#include "strsep.h"
+
+/* Get next token from string *stringp, where tokens are possibly empty
+ * strings separated by characters from delim.
+ *
+ * Writes NULLs into the string at *stringp to end tokens.
+ * delim need not remain constant from call to call.
+ * On return, *stringp points past the last NULL written (if there might
+ * be further tokens), or is NULL (if there are definitely no more tokens).
+ *
+ * If *stringp is NULL, strsep returns NULL.
+ */
+char *
+strsep(char **stringp, const char *delim)
+{
+	char *s;
+	const char *spanp;
+	int c, sc;
+	char *tok;
+
+	s = *stringp;
+	if (s == NULL)
+		return NULL;
+	for (tok = s;;) {
+		c = *s++;
+		spanp = delim;
+		do {
+			sc = *spanp++;
+			if (sc == c) {
+				if (c == 0)
+					s = NULL;
+				else
+					s[-1] = 0;
+				*stringp = s;
+				return tok;
+			}
+		} while (sc != 0);
+	}
+	/* NOTREACHED */
+}
-- 
2.49.0.vfs.0.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/2] test/strsep: add tests for function strsep()
  2025-05-16 13:22 [PATCH 1/2] eal: add strsep() for Windows builds Andre Muezerie
@ 2025-05-16 13:22 ` Andre Muezerie
  2025-07-21 20:39 ` [PATCH v2 1/2] eal: add strsep() for Windows builds Andre Muezerie
  2025-07-22 16:38 ` [PATCH v3 1/2] eal: add strsep() for Windows builds Andre Muezerie
  2 siblings, 0 replies; 8+ messages in thread
From: Andre Muezerie @ 2025-05-16 13:22 UTC (permalink / raw)
  Cc: dev, Andre Muezerie

This patch adds tests for the strsep() function.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 app/test/meson.build   |   1 +
 app/test/test_strsep.c | 108 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+)
 create mode 100644 app/test/test_strsep.c

diff --git a/app/test/meson.build b/app/test/meson.build
index b6285a6b45..ec5a1fbae3 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -184,6 +184,7 @@ source_file_deps = {
     'test_stack.c': ['stack'],
     'test_stack_perf.c': ['stack'],
     'test_string_fns.c': [],
+    'test_strsep.c': [],
     'test_table.c': ['table', 'pipeline', 'port'],
     'test_table_acl.c': ['net', 'table', 'pipeline', 'port'],
     'test_table_combined.c': ['table', 'pipeline', 'port'],
diff --git a/app/test/test_strsep.c b/app/test/test_strsep.c
new file mode 100644
index 0000000000..19df8f31ee
--- /dev/null
+++ b/app/test/test_strsep.c
@@ -0,0 +1,108 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (C) 2025 Microsoft Corporation
+ */
+
+#include <string.h>
+#include <rte_os_shim.h>
+
+#include "test.h"
+
+static int
+test_strsep_helper(const char *str, const char *delim,
+		   const char * const expected_tokens[], size_t expected_tokens_count)
+{
+	char *s = str != NULL ? strdup(str) : NULL;
+	const char *token;
+	for (size_t i = 0; i < expected_tokens_count; i++) {
+		token = strsep(&s, delim);
+		if (token == NULL) {
+			printf("Expected token '%s', got NULL\n", expected_tokens[i]);
+			free(s);
+			return TEST_FAILED;
+		}
+		if (strcmp(expected_tokens[i], token) != 0) {
+			printf("Expected '%s', got '%s'\n", expected_tokens[i], token);
+			free(s);
+			return TEST_FAILED;
+		}
+	}
+	/* Check that there are no more tokens left */
+	token = strsep(&s, delim);
+	if (token != NULL) {
+		printf("Expected NULL, got '%s'\n", token);
+		free(s);
+		return TEST_FAILED;
+	}
+	free(s);
+	return TEST_SUCCESS;
+}
+
+static int
+test_strsep_single_delimiter(void)
+{
+	const char *str = "hello,world";
+	const char *delim = ",";
+	static const char * const expected_tokens[] = { "hello", "world" };
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_multiple_delimiters(void)
+{
+	const char *str = "hello,world;this:is;a:test";
+	const char *delim = ",;:";
+	static const char * const expected_tokens[] = {"hello", "world", "this", "is", "a", "test"};
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_string_with_no_delimiters(void)
+{
+	const char *str = "helloworld";
+	const char *delim = ",";
+	static const char * const expected_tokens[] = {"helloworld"};
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_empty_string(void)
+{
+	const char *str = "";
+	const char *delim = ",";
+	static const char * const expected_tokens[] = {""};
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_null(void)
+{
+	const char *str = NULL;
+	const char *delim = ",";
+	static const char * const expected_tokens[] = {""};
+	const size_t expected_tokens_count = 0;
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static struct unit_test_suite test_suite = {
+	.suite_name = "Strsep test suite",
+	.unit_test_cases = {
+		TEST_CASE(test_strsep_single_delimiter),
+		TEST_CASE(test_strsep_multiple_delimiters),
+		TEST_CASE(test_strsep_string_with_no_delimiters),
+		TEST_CASE(test_strsep_empty_string),
+		TEST_CASE(test_strsep_null),
+		TEST_CASES_END()
+	}
+};
+
+static int
+test_strsep(void)
+{
+	return unit_test_suite_runner(&test_suite);
+}
+
+REGISTER_FAST_TEST(strsep_autotest, true, true, test_strsep);
-- 
2.49.0.vfs.0.3


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 1/2] eal: add strsep() for Windows builds
  2025-05-16 13:22 [PATCH 1/2] eal: add strsep() for Windows builds Andre Muezerie
  2025-05-16 13:22 ` [PATCH 2/2] test/strsep: add tests for function strsep() Andre Muezerie
@ 2025-07-21 20:39 ` Andre Muezerie
  2025-07-21 20:39   ` [PATCH v2 2/2] test/strsep: add tests for function strsep() Andre Muezerie
  2025-07-22 16:38 ` [PATCH v3 1/2] eal: add strsep() for Windows builds Andre Muezerie
  2 siblings, 1 reply; 8+ messages in thread
From: Andre Muezerie @ 2025-07-21 20:39 UTC (permalink / raw)
  To: andremue; +Cc: dev, dmitry.kozliuk, roretzla

Function strsep() is used by DPDK code but is not available on Windows.

This patch adds an implementation from NetBSD, unblocking code depending
on it from being compiled on Windows.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 lib/eal/windows/include/rte_os_shim.h |  1 +
 lib/eal/windows/include/strsep.h      | 12 +++++++
 lib/eal/windows/meson.build           |  1 +
 lib/eal/windows/strsep.c              | 47 +++++++++++++++++++++++++++
 4 files changed, 61 insertions(+)
 create mode 100644 lib/eal/windows/include/strsep.h
 create mode 100644 lib/eal/windows/strsep.c

diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
index 0e74eb19c7..9c3da554da 100644
--- a/lib/eal/windows/include/rte_os_shim.h
+++ b/lib/eal/windows/include/rte_os_shim.h
@@ -7,6 +7,7 @@
 
 #include <rte_os.h>
 #include <rte_windows.h>
+#include <strsep.h>
 
 /**
  * @file
diff --git a/lib/eal/windows/include/strsep.h b/lib/eal/windows/include/strsep.h
new file mode 100644
index 0000000000..1ff93f8cd0
--- /dev/null
+++ b/lib/eal/windows/include/strsep.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California.  All rights reserved.
+ */
+
+#ifndef _STRSEP_
+#define _STRSEP_
+
+char *
+strsep(char **stringp, const char *delim);
+
+#endif
diff --git a/lib/eal/windows/meson.build b/lib/eal/windows/meson.build
index c526ede405..b7c880d6b7 100644
--- a/lib/eal/windows/meson.build
+++ b/lib/eal/windows/meson.build
@@ -20,6 +20,7 @@ sources += files(
         'getline.c',
         'getopt.c',
         'rte_thread.c',
+        'strsep.c',
 )
 
 dpdk_conf.set10('RTE_EAL_NUMA_AWARE_HUGEPAGES', true)
diff --git a/lib/eal/windows/strsep.c b/lib/eal/windows/strsep.c
new file mode 100644
index 0000000000..fdc0688a07
--- /dev/null
+++ b/lib/eal/windows/strsep.c
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California.  All rights reserved.
+ */
+
+#include <string.h>
+
+#include "strsep.h"
+
+/* Get next token from string *stringp, where tokens are possibly empty
+ * strings separated by characters from delim.
+ *
+ * Writes NULLs into the string at *stringp to end tokens.
+ * delim need not remain constant from call to call.
+ * On return, *stringp points past the last NULL written (if there might
+ * be further tokens), or is NULL (if there are definitely no more tokens).
+ *
+ * If *stringp is NULL, strsep returns NULL.
+ */
+char *
+strsep(char **stringp, const char *delim)
+{
+	char *s;
+	const char *spanp;
+	int c, sc;
+	char *tok;
+
+	s = *stringp;
+	if (s == NULL)
+		return NULL;
+	for (tok = s;;) {
+		c = *s++;
+		spanp = delim;
+		do {
+			sc = *spanp++;
+			if (sc == c) {
+				if (c == 0)
+					s = NULL;
+				else
+					s[-1] = 0;
+				*stringp = s;
+				return tok;
+			}
+		} while (sc != 0);
+	}
+	/* NOTREACHED */
+}
-- 
2.50.1.vfs.0.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 2/2] test/strsep: add tests for function strsep()
  2025-07-21 20:39 ` [PATCH v2 1/2] eal: add strsep() for Windows builds Andre Muezerie
@ 2025-07-21 20:39   ` Andre Muezerie
  2025-07-22 14:30     ` David Marchand
  0 siblings, 1 reply; 8+ messages in thread
From: Andre Muezerie @ 2025-07-21 20:39 UTC (permalink / raw)
  To: andremue; +Cc: dev, dmitry.kozliuk, roretzla

This patch adds tests for the strsep() function.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 app/test/meson.build   |   1 +
 app/test/test_strsep.c | 108 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+)
 create mode 100644 app/test/test_strsep.c

diff --git a/app/test/meson.build b/app/test/meson.build
index 7d38f51918..8cc960c540 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -187,6 +187,7 @@ source_file_deps = {
     'test_stack.c': ['stack'],
     'test_stack_perf.c': ['stack'],
     'test_string_fns.c': [],
+    'test_strsep.c': [],
     'test_table.c': ['table', 'pipeline', 'port'],
     'test_table_acl.c': ['net', 'table', 'pipeline', 'port'],
     'test_table_combined.c': ['table', 'pipeline', 'port'],
diff --git a/app/test/test_strsep.c b/app/test/test_strsep.c
new file mode 100644
index 0000000000..19df8f31ee
--- /dev/null
+++ b/app/test/test_strsep.c
@@ -0,0 +1,108 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (C) 2025 Microsoft Corporation
+ */
+
+#include <string.h>
+#include <rte_os_shim.h>
+
+#include "test.h"
+
+static int
+test_strsep_helper(const char *str, const char *delim,
+		   const char * const expected_tokens[], size_t expected_tokens_count)
+{
+	char *s = str != NULL ? strdup(str) : NULL;
+	const char *token;
+	for (size_t i = 0; i < expected_tokens_count; i++) {
+		token = strsep(&s, delim);
+		if (token == NULL) {
+			printf("Expected token '%s', got NULL\n", expected_tokens[i]);
+			free(s);
+			return TEST_FAILED;
+		}
+		if (strcmp(expected_tokens[i], token) != 0) {
+			printf("Expected '%s', got '%s'\n", expected_tokens[i], token);
+			free(s);
+			return TEST_FAILED;
+		}
+	}
+	/* Check that there are no more tokens left */
+	token = strsep(&s, delim);
+	if (token != NULL) {
+		printf("Expected NULL, got '%s'\n", token);
+		free(s);
+		return TEST_FAILED;
+	}
+	free(s);
+	return TEST_SUCCESS;
+}
+
+static int
+test_strsep_single_delimiter(void)
+{
+	const char *str = "hello,world";
+	const char *delim = ",";
+	static const char * const expected_tokens[] = { "hello", "world" };
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_multiple_delimiters(void)
+{
+	const char *str = "hello,world;this:is;a:test";
+	const char *delim = ",;:";
+	static const char * const expected_tokens[] = {"hello", "world", "this", "is", "a", "test"};
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_string_with_no_delimiters(void)
+{
+	const char *str = "helloworld";
+	const char *delim = ",";
+	static const char * const expected_tokens[] = {"helloworld"};
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_empty_string(void)
+{
+	const char *str = "";
+	const char *delim = ",";
+	static const char * const expected_tokens[] = {""};
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_null(void)
+{
+	const char *str = NULL;
+	const char *delim = ",";
+	static const char * const expected_tokens[] = {""};
+	const size_t expected_tokens_count = 0;
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static struct unit_test_suite test_suite = {
+	.suite_name = "Strsep test suite",
+	.unit_test_cases = {
+		TEST_CASE(test_strsep_single_delimiter),
+		TEST_CASE(test_strsep_multiple_delimiters),
+		TEST_CASE(test_strsep_string_with_no_delimiters),
+		TEST_CASE(test_strsep_empty_string),
+		TEST_CASE(test_strsep_null),
+		TEST_CASES_END()
+	}
+};
+
+static int
+test_strsep(void)
+{
+	return unit_test_suite_runner(&test_suite);
+}
+
+REGISTER_FAST_TEST(strsep_autotest, true, true, test_strsep);
-- 
2.50.1.vfs.0.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 2/2] test/strsep: add tests for function strsep()
  2025-07-21 20:39   ` [PATCH v2 2/2] test/strsep: add tests for function strsep() Andre Muezerie
@ 2025-07-22 14:30     ` David Marchand
  2025-07-22 16:37       ` Andre Muezerie
  0 siblings, 1 reply; 8+ messages in thread
From: David Marchand @ 2025-07-22 14:30 UTC (permalink / raw)
  To: Andre Muezerie; +Cc: dev, dmitry.kozliuk, roretzla

Hello Andre,

On Mon, Jul 21, 2025 at 10:39 PM Andre Muezerie
<andremue@linux.microsoft.com> wrote:
> diff --git a/app/test/test_strsep.c b/app/test/test_strsep.c
> new file mode 100644
> index 0000000000..19df8f31ee
> --- /dev/null
> +++ b/app/test/test_strsep.c
> @@ -0,0 +1,108 @@
> +/* SPDX-License-Identifier: BSD-3-Clause
> + * Copyright (C) 2025 Microsoft Corporation
> + */
> +
> +#include <string.h>
> +#include <rte_os_shim.h>
> +
> +#include "test.h"
> +
> +static int
> +test_strsep_helper(const char *str, const char *delim,
> +                  const char * const expected_tokens[], size_t expected_tokens_count)
> +{
> +       char *s = str != NULL ? strdup(str) : NULL;
> +       const char *token;
> +       for (size_t i = 0; i < expected_tokens_count; i++) {
> +               token = strsep(&s, delim);

The reference to the dupped string gets lost, since calling strsep updates s.
Caught by ASan:

==49990==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 51 byte(s) in 4 object(s) allocated from:
    #0 0x5632dd8cbe83 in strdup
(/home/runner/work/dpdk/dpdk/build/app/dpdk-test+0x271e83) (BuildId:
01a7571ab7814bc83923733b3dae34d28e6ff3e1)
    #1 0x5632de39bad2 in test_strsep_helper
/home/runner/work/dpdk/dpdk/build/../app/test/test_strsep.c:14:26
    #2 0x5632dd91d86f in unit_test_suite_runner
/home/runner/work/dpdk/dpdk/build/../app/test/test.c:364:20
    #3 0x5632dd91c2b0 in cmd_autotest_parsed
/home/runner/work/dpdk/dpdk/build/../app/test/commands.c:68:10
    #4 0x7fba8b3bfc58 in __cmdline_parse
/home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline_parse.c:296:3
    #5 0x7fba8b3bfc58 in cmdline_parse
/home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline_parse.c:305:9
    #6 0x7fba8b3bcbf7 in cmdline_valid_buffer
/home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline.c:25:8
    #7 0x7fba8b3c50ec in rdline_char_in
/home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline_rdline.c:456:5
    #8 0x7fba8b3bcfde in cmdline_in
/home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline.c:154:9
    #9 0x5632dd91d2ba in main
/home/runner/work/dpdk/dpdk/build/../app/test/test.c:231:15
    #10 0x7fba89a29d8f in __libc_start_call_main
csu/../sysdeps/nptl/libc_start_call_main.h:58:16

SUMMARY: AddressSanitizer: 51 byte(s) leaked in 4 allocation(s).
------------------------------------------------------------------------------


-- 
David Marchand


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 2/2] test/strsep: add tests for function strsep()
  2025-07-22 14:30     ` David Marchand
@ 2025-07-22 16:37       ` Andre Muezerie
  0 siblings, 0 replies; 8+ messages in thread
From: Andre Muezerie @ 2025-07-22 16:37 UTC (permalink / raw)
  To: David Marchand; +Cc: dev, dmitry.kozliuk, roretzla

On Tue, Jul 22, 2025 at 04:30:57PM +0200, David Marchand wrote:
> Hello Andre,
> 
> On Mon, Jul 21, 2025 at 10:39 PM Andre Muezerie
> <andremue@linux.microsoft.com> wrote:
> > diff --git a/app/test/test_strsep.c b/app/test/test_strsep.c
> > new file mode 100644
> > index 0000000000..19df8f31ee
> > --- /dev/null
> > +++ b/app/test/test_strsep.c
> > @@ -0,0 +1,108 @@
> > +/* SPDX-License-Identifier: BSD-3-Clause
> > + * Copyright (C) 2025 Microsoft Corporation
> > + */
> > +
> > +#include <string.h>
> > +#include <rte_os_shim.h>
> > +
> > +#include "test.h"
> > +
> > +static int
> > +test_strsep_helper(const char *str, const char *delim,
> > +                  const char * const expected_tokens[], size_t expected_tokens_count)
> > +{
> > +       char *s = str != NULL ? strdup(str) : NULL;
> > +       const char *token;
> > +       for (size_t i = 0; i < expected_tokens_count; i++) {
> > +               token = strsep(&s, delim);
> 
> The reference to the dupped string gets lost, since calling strsep updates s.
> Caught by ASan:
> 
> ==49990==ERROR: LeakSanitizer: detected memory leaks
> 
> Direct leak of 51 byte(s) in 4 object(s) allocated from:
>     #0 0x5632dd8cbe83 in strdup
> (/home/runner/work/dpdk/dpdk/build/app/dpdk-test+0x271e83) (BuildId:
> 01a7571ab7814bc83923733b3dae34d28e6ff3e1)
>     #1 0x5632de39bad2 in test_strsep_helper
> /home/runner/work/dpdk/dpdk/build/../app/test/test_strsep.c:14:26
>     #2 0x5632dd91d86f in unit_test_suite_runner
> /home/runner/work/dpdk/dpdk/build/../app/test/test.c:364:20
>     #3 0x5632dd91c2b0 in cmd_autotest_parsed
> /home/runner/work/dpdk/dpdk/build/../app/test/commands.c:68:10
>     #4 0x7fba8b3bfc58 in __cmdline_parse
> /home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline_parse.c:296:3
>     #5 0x7fba8b3bfc58 in cmdline_parse
> /home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline_parse.c:305:9
>     #6 0x7fba8b3bcbf7 in cmdline_valid_buffer
> /home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline.c:25:8
>     #7 0x7fba8b3c50ec in rdline_char_in
> /home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline_rdline.c:456:5
>     #8 0x7fba8b3bcfde in cmdline_in
> /home/runner/work/dpdk/dpdk/build/../lib/cmdline/cmdline.c:154:9
>     #9 0x5632dd91d2ba in main
> /home/runner/work/dpdk/dpdk/build/../app/test/test.c:231:15
>     #10 0x7fba89a29d8f in __libc_start_call_main
> csu/../sysdeps/nptl/libc_start_call_main.h:58:16
> 
> SUMMARY: AddressSanitizer: 51 byte(s) leaked in 4 allocation(s).
> ------------------------------------------------------------------------------
> 
> 
> -- 
> David Marchand

Thanks for sending this info. I'll send out a fixed version.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3 1/2] eal: add strsep() for Windows builds
  2025-05-16 13:22 [PATCH 1/2] eal: add strsep() for Windows builds Andre Muezerie
  2025-05-16 13:22 ` [PATCH 2/2] test/strsep: add tests for function strsep() Andre Muezerie
  2025-07-21 20:39 ` [PATCH v2 1/2] eal: add strsep() for Windows builds Andre Muezerie
@ 2025-07-22 16:38 ` Andre Muezerie
  2025-07-22 16:38   ` [PATCH v3 2/2] test/strsep: add tests for function strsep() Andre Muezerie
  2 siblings, 1 reply; 8+ messages in thread
From: Andre Muezerie @ 2025-07-22 16:38 UTC (permalink / raw)
  To: andremue; +Cc: dev, dmitry.kozliuk, roretzla

Function strsep() is used by DPDK code but is not available on Windows.

This patch adds an implementation from NetBSD, unblocking code depending
on it from being compiled on Windows.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 lib/eal/windows/include/rte_os_shim.h |  1 +
 lib/eal/windows/include/strsep.h      | 12 +++++++
 lib/eal/windows/meson.build           |  1 +
 lib/eal/windows/strsep.c              | 47 +++++++++++++++++++++++++++
 4 files changed, 61 insertions(+)
 create mode 100644 lib/eal/windows/include/strsep.h
 create mode 100644 lib/eal/windows/strsep.c

diff --git a/lib/eal/windows/include/rte_os_shim.h b/lib/eal/windows/include/rte_os_shim.h
index 0e74eb19c7..9c3da554da 100644
--- a/lib/eal/windows/include/rte_os_shim.h
+++ b/lib/eal/windows/include/rte_os_shim.h
@@ -7,6 +7,7 @@
 
 #include <rte_os.h>
 #include <rte_windows.h>
+#include <strsep.h>
 
 /**
  * @file
diff --git a/lib/eal/windows/include/strsep.h b/lib/eal/windows/include/strsep.h
new file mode 100644
index 0000000000..1ff93f8cd0
--- /dev/null
+++ b/lib/eal/windows/include/strsep.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California.  All rights reserved.
+ */
+
+#ifndef _STRSEP_
+#define _STRSEP_
+
+char *
+strsep(char **stringp, const char *delim);
+
+#endif
diff --git a/lib/eal/windows/meson.build b/lib/eal/windows/meson.build
index c526ede405..b7c880d6b7 100644
--- a/lib/eal/windows/meson.build
+++ b/lib/eal/windows/meson.build
@@ -20,6 +20,7 @@ sources += files(
         'getline.c',
         'getopt.c',
         'rte_thread.c',
+        'strsep.c',
 )
 
 dpdk_conf.set10('RTE_EAL_NUMA_AWARE_HUGEPAGES', true)
diff --git a/lib/eal/windows/strsep.c b/lib/eal/windows/strsep.c
new file mode 100644
index 0000000000..fdc0688a07
--- /dev/null
+++ b/lib/eal/windows/strsep.c
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (c) 1990, 1993
+ * The Regents of the University of California.  All rights reserved.
+ */
+
+#include <string.h>
+
+#include "strsep.h"
+
+/* Get next token from string *stringp, where tokens are possibly empty
+ * strings separated by characters from delim.
+ *
+ * Writes NULLs into the string at *stringp to end tokens.
+ * delim need not remain constant from call to call.
+ * On return, *stringp points past the last NULL written (if there might
+ * be further tokens), or is NULL (if there are definitely no more tokens).
+ *
+ * If *stringp is NULL, strsep returns NULL.
+ */
+char *
+strsep(char **stringp, const char *delim)
+{
+	char *s;
+	const char *spanp;
+	int c, sc;
+	char *tok;
+
+	s = *stringp;
+	if (s == NULL)
+		return NULL;
+	for (tok = s;;) {
+		c = *s++;
+		spanp = delim;
+		do {
+			sc = *spanp++;
+			if (sc == c) {
+				if (c == 0)
+					s = NULL;
+				else
+					s[-1] = 0;
+				*stringp = s;
+				return tok;
+			}
+		} while (sc != 0);
+	}
+	/* NOTREACHED */
+}
-- 
2.50.1.vfs.0.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3 2/2] test/strsep: add tests for function strsep()
  2025-07-22 16:38 ` [PATCH v3 1/2] eal: add strsep() for Windows builds Andre Muezerie
@ 2025-07-22 16:38   ` Andre Muezerie
  0 siblings, 0 replies; 8+ messages in thread
From: Andre Muezerie @ 2025-07-22 16:38 UTC (permalink / raw)
  To: andremue; +Cc: dev, dmitry.kozliuk, roretzla

This patch adds tests for the strsep() function.

Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
 app/test/meson.build   |   1 +
 app/test/test_strsep.c | 109 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)
 create mode 100644 app/test/test_strsep.c

diff --git a/app/test/meson.build b/app/test/meson.build
index 7d38f51918..8cc960c540 100644
--- a/app/test/meson.build
+++ b/app/test/meson.build
@@ -187,6 +187,7 @@ source_file_deps = {
     'test_stack.c': ['stack'],
     'test_stack_perf.c': ['stack'],
     'test_string_fns.c': [],
+    'test_strsep.c': [],
     'test_table.c': ['table', 'pipeline', 'port'],
     'test_table_acl.c': ['net', 'table', 'pipeline', 'port'],
     'test_table_combined.c': ['table', 'pipeline', 'port'],
diff --git a/app/test/test_strsep.c b/app/test/test_strsep.c
new file mode 100644
index 0000000000..6c2ff67c18
--- /dev/null
+++ b/app/test/test_strsep.c
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright (C) 2025 Microsoft Corporation
+ */
+
+#include <string.h>
+#include <rte_os_shim.h>
+
+#include "test.h"
+
+static int
+test_strsep_helper(const char *str, const char *delim,
+		   const char * const expected_tokens[], size_t expected_tokens_count)
+{
+	char *s_dup = str != NULL ? strdup(str) : NULL;
+	char *s = s_dup;
+	const char *token;
+	for (size_t i = 0; i < expected_tokens_count; i++) {
+		token = strsep(&s, delim);
+		if (token == NULL) {
+			printf("Expected token '%s', got NULL\n", expected_tokens[i]);
+			free(s_dup);
+			return TEST_FAILED;
+		}
+		if (strcmp(expected_tokens[i], token) != 0) {
+			printf("Expected '%s', got '%s'\n", expected_tokens[i], token);
+			free(s_dup);
+			return TEST_FAILED;
+		}
+	}
+	/* Check that there are no more tokens left */
+	token = strsep(&s, delim);
+	if (token != NULL) {
+		printf("Expected NULL, got '%s'\n", token);
+		free(s_dup);
+		return TEST_FAILED;
+	}
+	free(s_dup);
+	return TEST_SUCCESS;
+}
+
+static int
+test_strsep_single_delimiter(void)
+{
+	const char *str = "hello,world";
+	const char *delim = ",";
+	static const char * const expected_tokens[] = { "hello", "world" };
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_multiple_delimiters(void)
+{
+	const char *str = "hello,world;this:is;a:test";
+	const char *delim = ",;:";
+	static const char * const expected_tokens[] = {"hello", "world", "this", "is", "a", "test"};
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_string_with_no_delimiters(void)
+{
+	const char *str = "helloworld";
+	const char *delim = ",";
+	static const char * const expected_tokens[] = {"helloworld"};
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_empty_string(void)
+{
+	const char *str = "";
+	const char *delim = ",";
+	static const char * const expected_tokens[] = {""};
+	const size_t expected_tokens_count = RTE_DIM(expected_tokens);
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static int
+test_strsep_null(void)
+{
+	const char *str = NULL;
+	const char *delim = ",";
+	static const char * const expected_tokens[] = {""};
+	const size_t expected_tokens_count = 0;
+	return test_strsep_helper(str, delim, expected_tokens, expected_tokens_count);
+}
+
+static struct unit_test_suite test_suite = {
+	.suite_name = "Strsep test suite",
+	.unit_test_cases = {
+		TEST_CASE(test_strsep_single_delimiter),
+		TEST_CASE(test_strsep_multiple_delimiters),
+		TEST_CASE(test_strsep_string_with_no_delimiters),
+		TEST_CASE(test_strsep_empty_string),
+		TEST_CASE(test_strsep_null),
+		TEST_CASES_END()
+	}
+};
+
+static int
+test_strsep(void)
+{
+	return unit_test_suite_runner(&test_suite);
+}
+
+REGISTER_FAST_TEST(strsep_autotest, true, true, test_strsep);
-- 
2.50.1.vfs.0.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-07-22 16:39 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-16 13:22 [PATCH 1/2] eal: add strsep() for Windows builds Andre Muezerie
2025-05-16 13:22 ` [PATCH 2/2] test/strsep: add tests for function strsep() Andre Muezerie
2025-07-21 20:39 ` [PATCH v2 1/2] eal: add strsep() for Windows builds Andre Muezerie
2025-07-21 20:39   ` [PATCH v2 2/2] test/strsep: add tests for function strsep() Andre Muezerie
2025-07-22 14:30     ` David Marchand
2025-07-22 16:37       ` Andre Muezerie
2025-07-22 16:38 ` [PATCH v3 1/2] eal: add strsep() for Windows builds Andre Muezerie
2025-07-22 16:38   ` [PATCH v3 2/2] test/strsep: add tests for function strsep() Andre Muezerie

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).