public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] string: move __compiletime_strlen() to string.h
@ 2026-02-05 10:05 Xie Yuanbin
  2026-02-05 10:05 ` [PATCH 2/2] powerpc/text-patching: Fix possible stringop-overread compilation error Xie Yuanbin
  0 siblings, 1 reply; 9+ messages in thread
From: Xie Yuanbin @ 2026-02-05 10:05 UTC (permalink / raw)
  To: maddy, mpe, npiggin, chleroy, kees, andy
  Cc: linuxppc-dev, linux-kernel, linux-hardening, lilinjie8, liaohua4,
	xieyuanbin1

Move __compiletime_strlen() to string.h, so that others can use.

Signed-off-by: Xie Yuanbin <xieyuanbin1@huawei.com>
---
 include/linux/fortify-string.h | 15 ---------------
 include/linux/string.h         | 15 +++++++++++++++
 lib/tests/fortify_kunit.c      |  3 ++-
 3 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h
index 171982e53c9a..c181dac26353 100644
--- a/include/linux/fortify-string.h
+++ b/include/linux/fortify-string.h
@@ -58,21 +58,6 @@ void __read_overflow2_field(size_t avail, size_t wanted) __compiletime_warning("
 void __write_overflow(void) __compiletime_error("detected write beyond size of object (1st parameter)");
 void __write_overflow_field(size_t avail, size_t wanted) __compiletime_warning("detected write beyond size of field (1st parameter); maybe use struct_group()?");
 
-#define __compiletime_strlen(p)					\
-({								\
-	char *__p = (char *)(p);				\
-	size_t __ret = SIZE_MAX;				\
-	const size_t __p_size = __member_size(p);		\
-	if (__p_size != SIZE_MAX &&				\
-	    __builtin_constant_p(*__p)) {			\
-		size_t __p_len = __p_size - 1;			\
-		if (__builtin_constant_p(__p[__p_len]) &&	\
-		    __p[__p_len] == '\0')			\
-			__ret = __builtin_strlen(__p);		\
-	}							\
-	__ret;							\
-})
-
 #if defined(__SANITIZE_ADDRESS__)
 
 #if !defined(CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX) && !defined(CONFIG_GENERIC_ENTRY)
diff --git a/include/linux/string.h b/include/linux/string.h
index 1b564c36d721..fbae7d99bb6f 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -19,6 +19,21 @@ extern void *memdup_user(const void __user *, size_t) __realloc_size(2);
 extern void *vmemdup_user(const void __user *, size_t) __realloc_size(2);
 extern void *memdup_user_nul(const void __user *, size_t);
 
+#define __compiletime_strlen(p)					\
+({								\
+	char *__p = (char *)(p);				\
+	size_t __ret = SIZE_MAX;				\
+	const size_t __p_size = __member_size(p);		\
+	if (__p_size != SIZE_MAX &&				\
+	    __builtin_constant_p(*__p)) {			\
+		size_t __p_len = __p_size - 1;			\
+		if (__builtin_constant_p(__p[__p_len]) &&	\
+		    __p[__p_len] == '\0')			\
+			__ret = __builtin_strlen(__p);		\
+	}							\
+	__ret;							\
+})
+
 /**
  * memdup_array_user - duplicate array from user space
  * @src: source address in user space
diff --git a/lib/tests/fortify_kunit.c b/lib/tests/fortify_kunit.c
index fc9c76f026d6..86181e3bd994 100644
--- a/lib/tests/fortify_kunit.c
+++ b/lib/tests/fortify_kunit.c
@@ -49,7 +49,8 @@ void fortify_add_kunit_error(int write);
 #include <linux/vmalloc.h>
 
 /* Handle being built without CONFIG_FORTIFY_SOURCE */
-#ifndef __compiletime_strlen
+#if defined(__NO_FORTIFY) || !defined(__OPTIMIZE__) || !defined(CONFIG_FORTIFY_SOURCE)
+# undef __compiletime_strlen
 # define __compiletime_strlen __builtin_strlen
 #endif
 
-- 
2.51.0


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

end of thread, other threads:[~2026-02-09 14:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-05 10:05 [PATCH 1/2] string: move __compiletime_strlen() to string.h Xie Yuanbin
2026-02-05 10:05 ` [PATCH 2/2] powerpc/text-patching: Fix possible stringop-overread compilation error Xie Yuanbin
2026-02-05 16:40   ` Andy Shevchenko
2026-02-06 11:14     ` Xie Yuanbin
2026-02-06 18:26   ` Kees Cook
2026-02-06 19:53     ` Christophe Leroy (CS GROUP)
2026-02-09 13:25       ` Xie Yuanbin
2026-02-09 13:41         ` Christophe Leroy (CS GROUP)
2026-02-09 14:11           ` Xie Yuanbin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox