Skip to contentSkip to conversation

string: Add missing const

PATCH In review Alejandro Colomar 3 participants 5 messages Nov 25 – Nov 26 Generic string library

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 string: Add missing const

This pointer is derived from other const pointers.

---

Hi,

This was found while changing some <linux/string.h> APIs to preserve the const qualifier in the return type.

Have a lovely night!
Alex

 lib/tests/string_kunit.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tests/string_kunit.c +1 -1
index f84a60d4414c..2b681089c200 100644
@@ -233,7 +233,7 @@ static void string_test_strnchr(struct kunit *test)
 {
 	const char *test_string = "abcdefghijkl";
 	const char *empty_string = "";
-	char *result;
+	const char *result;
 	int i, j;
 
 	for (i = 0; i < strlen(test_string) + 1; i++) {
Signature · 1 line
2.55.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Alejandro Colomar

This pointer is derived from other const pointers.

Constification almost always is a good thing, if this builds and works as expected,

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw

Hi Andy,

Quoted 8 lines

Date: 2026-11-26 17:20:11+0200
From: Andy Shevchenko <[email protected]>

Quoted 1 line · Alejandro Colomar

This pointer is derived from other const pointers.

Constification almost always is a good thing, if this builds and works as expected,

Yup, I built it as part of a larger branch. I didn't run the tests, but this should be a compile-time-only thing, so I don't think it's necessary.

Quoted 2 lines

Thanks!

Have a lovely day!
Alex

Signature · 1 line
<https://www.alejandro-colomar.es>
#3 lore raw
Quoted 9 lines · Alejandro Colomar
Quoted 5 lines · Alejandro Colomar
Quoted 1 line

This pointer is derived from other const pointers.

Constification almost always is a good thing, if this builds and works as expected,

Yup, I built it as part of a larger branch. I didn't run the tests, but this should be a compile-time-only thing, so I don't think it's necessary.

Running kunit test is so-o easy! Just run

./tools/testing/kunit/kunit.py run string

It will fail if compilation fails, so it's 2-in-1.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Hi Andy,

Quoted 14 lines

Date: 2026-11-26 22:51:57+0200
From: Andy Shevchenko <[email protected]>

Quoted 9 lines · Alejandro Colomar
Quoted 5 lines · Alejandro Colomar
Quoted 1 line

This pointer is derived from other const pointers.

Constification almost always is a good thing, if this builds and works as expected,

Yup, I built it as part of a larger branch. I didn't run the tests, but this should be a compile-time-only thing, so I don't think it's necessary.

Quoted 5 lines

Running kunit test is so-o easy! Just run

./tools/testing/kunit/kunit.py run string

It will fail if compilation fails, so it's 2-in-1.

I've passed the tests now; see below.

Have a lovely night!
Alex

$ git log -1
	commit 9331ce2afba1a44bb84f371581478a9ac5ee7380 (HEAD -> qchar)
	Author: Alejandro Colomar <[email protected]>
	Date:   2026-11-25 19:25:10 +0100

string: Add missing const

This pointer is derived from other const pointers.

$ cp ../kunitconfig .kunitconfig
$ cat .kunitconfig
	CONFIG_KUNIT=y
	CONFIG_STRING_KUNIT_TEST=y
$ mkdir -p ~/tmp/const-string1
$ export TMPDIR=$HOME/tmp/const-string1/
$ ./tools/testing/kunit/kunit.py run "string*"
	[22:47:41] Configuring KUnit Kernel ...
	Generating .config ...
	Populating config with:
	$ make ARCH=um O=.kunit olddefconfig
	[22:47:44] Building KUnit Kernel ...
	Populating config with:
	$ make ARCH=um O=.kunit olddefconfig
	Building with:
	$ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=24
	../drivers/base/dd.c: In function ‘__device_set_driver_override’:
	../drivers/base/dd.c:406:20: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
	  406 |                 cp = strnchr(s, len, '\n');
	      |                    ^
	[22:48:01] Starting KUnit Kernel (1/1)...
	[22:48:01] ============================================================
	Running tests with:
	$ .kunit/linux 'kunit.filter_glob=string*' kunit.enable=1 mem=1G console=tty kunit_shutdown=halt
	[22:48:02] ============= string-stream-test (12 subtests) =============
	[22:48:02] [PASSED] string_stream_managed_init_test
	[22:48:02] [PASSED] string_stream_unmanaged_init_test
	[22:48:02] [PASSED] string_stream_managed_free_test
	[22:48:02] [PASSED] string_stream_resource_free_test
	[22:48:02] [PASSED] string_stream_line_add_test
	[22:48:02] [PASSED] string_stream_variable_length_line_test
	[22:48:02] [PASSED] string_stream_append_test
	[22:48:02] [PASSED] string_stream_append_auto_newline_test
	[22:48:02] [PASSED] string_stream_append_empty_string_test
	[22:48:02] [PASSED] string_stream_no_auto_newline_test
	[22:48:02] [PASSED] string_stream_auto_newline_test
	[22:48:02] [PASSED] string_stream_performance_test
	[22:48:02] =============== [PASSED] string-stream-test ================
	[22:48:02] =================== string (29 subtests) ===================
	[22:48:02] [PASSED] string_test_memset16
	[22:48:02] [PASSED] string_test_memset32
	[22:48:02] [PASSED] string_test_memset64
	[22:48:02] [PASSED] string_test_strlen
	[22:48:02] [PASSED] string_test_strnlen
	[22:48:02] [PASSED] string_test_strchr
	[22:48:02] [PASSED] string_test_strnchr
	[22:48:02] [PASSED] string_test_strrchr
	[22:48:02] [PASSED] string_test_strspn
	[22:48:02] [PASSED] string_test_streq
	[22:48:02] [PASSED] string_test_strcmp
	[22:48:02] [PASSED] string_test_strcmp_long_strings
	[22:48:02] [PASSED] string_test_strncmp
	[22:48:02] [PASSED] string_test_strncmp_long_strings
	[22:48:02] [PASSED] string_test_strcasecmp
	[22:48:02] [PASSED] string_test_strcasecmp_long_strings
	[22:48:02] [PASSED] string_test_strncasecmp
	[22:48:02] [PASSED] string_test_strncasecmp_long_strings
	[22:48:02] [PASSED] string_test_strscpy
	[22:48:02] [PASSED] string_test_strcat
	[22:48:02] [PASSED] string_test_strncat
	[22:48:02] [PASSED] string_test_strlcat
	[22:48:02] [PASSED] string_test_strtomem
	[22:48:02] [PASSED] string_test_memtostr
	[22:48:02] [PASSED] string_test_strends
	[22:48:02] [SKIPPED] string_bench_strlen (not enabled)
	[22:48:02] [SKIPPED] string_bench_strnlen (not enabled)
	[22:48:02] [SKIPPED] string_bench_strchr (not enabled)
	[22:48:02] [SKIPPED] string_bench_strrchr (not enabled)
	[22:48:02] ===================== [PASSED] string ======================
	[22:48:02] =============== string_helpers (4 subtests) ================
	[22:48:02] [PASSED] test_get_size
	[22:48:02] [PASSED] test_upper_lower
	[22:48:02] [PASSED] test_unescape
	[22:48:02] [PASSED] test_escape
	[22:48:02] ================= [PASSED] string_helpers ==================
	[22:48:02] ============================================================
	[22:48:02] Testing complete. Ran 45 tests: passed: 41, skipped: 4
	[22:48:02] Elapsed time: 20.674s total, 3.447s configuring, 17.058s building, 0.128s running
Signature · 1 line
<https://www.alejandro-colomar.es>