[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260205100517.292858-1-xieyuanbin1@huawei.com>
Date: Thu, 5 Feb 2026 18:05:16 +0800
From: Xie Yuanbin <xieyuanbin1@...wei.com>
To: <maddy@...ux.ibm.com>, <mpe@...erman.id.au>, <npiggin@...il.com>,
<chleroy@...nel.org>, <kees@...nel.org>, <andy@...nel.org>
CC: <linuxppc-dev@...ts.ozlabs.org>, <linux-kernel@...r.kernel.org>,
<linux-hardening@...r.kernel.org>, <lilinjie8@...wei.com>,
<liaohua4@...wei.com>, <xieyuanbin1@...wei.com>
Subject: [PATCH 1/2] string: move __compiletime_strlen() to string.h
Move __compiletime_strlen() to string.h, so that others can use.
Signed-off-by: Xie Yuanbin <xieyuanbin1@...wei.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
Powered by blists - more mailing lists