mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] string: add strlcat support
@ 2024-10-16  8:56 Ahmad Fatoum
  2024-10-16  8:56 ` [PATCH 2/2] string: support overriding strchr/strrchr/strstr macros Ahmad Fatoum
  2024-10-18  8:52 ` [PATCH 1/2] string: add strlcat support Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-10-16  8:56 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We have a lot of kernel string functions already for use by common,
driver and board code, but we are lacking strlcat.

Add it to simplify porting code using it.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/linux/string.h |  3 +++
 lib/string.c           | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 0d046f783280..4aa11555a005 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -55,6 +55,9 @@ extern char * strcat(char *, const char *);
 #ifndef __HAVE_ARCH_STRNCAT
 extern char * strncat(char *, const char *, __kernel_size_t);
 #endif
+#ifndef __HAVE_ARCH_STRLCAT
+extern __kernel_size_t strlcat(char *dest, const char *src, __kernel_size_t count);
+#endif
 #ifndef __HAVE_ARCH_STRCMP
 extern int strcmp(const char *,const char *);
 #endif
diff --git a/lib/string.c b/lib/string.c
index 50c2016c2bd8..cab543baf38d 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -270,6 +270,27 @@ char * strncat(char *dest, const char *src, size_t count)
 #endif
 EXPORT_SYMBOL(strncat);
 
+#ifndef __HAVE_ARCH_STRLCAT
+size_t strlcat(char *dest, const char *src, size_t count)
+{
+	size_t dsize = strlen(dest);
+	size_t len = strlen(src);
+	size_t res = dsize + len;
+
+	/* This would be a bug */
+	BUG_ON(dsize >= count);
+
+	dest += dsize;
+	count -= dsize;
+	if (len >= count)
+		len = count-1;
+	__builtin_memcpy(dest, src, len);
+	dest[len] = 0;
+	return res;
+}
+EXPORT_SYMBOL(strlcat);
+#endif
+
 #ifndef __HAVE_ARCH_STRCMP
 /**
  * strcmp - Compare two strings
-- 
2.39.5




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

* [PATCH 2/2] string: support overriding strchr/strrchr/strstr macros
  2024-10-16  8:56 [PATCH 1/2] string: add strlcat support Ahmad Fatoum
@ 2024-10-16  8:56 ` Ahmad Fatoum
  2024-10-18  8:52 ` [PATCH 1/2] string: add strlcat support Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-10-16  8:56 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Architecture code may want to override these functions, e.g.
in the case that they are overridden due to use of libfuzzer.

Add #ifndefs to support that and move up the architecture-specific
header, so the overrides may work.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/linux/string.h | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/include/linux/string.h b/include/linux/string.h
index 4aa11555a005..3b8775c9a57d 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -6,6 +6,10 @@
 #include <linux/types.h>	/* for size_t */
 #include <linux/stddef.h>	/* for NULL */
 #include <linux/overflow.h>	/* for array_size */
+/*
+ * Include machine specific inline routines
+ */
+#include <asm/string.h>
 
 #ifdef __cplusplus
 extern "C" {
@@ -16,27 +20,28 @@ extern char * strsep(char **,const char *);
 extern __kernel_size_t strspn(const char *,const char *);
 
 
+#ifndef strchr
 #define strchr(s, c) ({ \
 	(typeof(&(s)[0]))(_strchr((s), c)); \
 	})
+#endif
 
+#ifndef strrchr
 #define strrchr(s, c) ({ \
 	(typeof(&(s)[0]))(_strrchr((s), c)); \
 	})
+#endif
 
+#ifndef strstr
 #define strstr(s1, s2) ({ \
 	(typeof(&(s1)[0]))(_strstr((s1), (s2))); \
 	})
+#endif
 
 #ifndef __HAVE_ARCH_STRCHRNUL
 extern char * strchrnul(const char *,int);
 #endif
 
-/*
- * Include machine specific inline routines
- */
-#include <asm/string.h>
-
 #ifndef __HAVE_ARCH_STRCPY
 extern char * strcpy(char *,const char *);
 #endif
-- 
2.39.5




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

* Re: [PATCH 1/2] string: add strlcat support
  2024-10-16  8:56 [PATCH 1/2] string: add strlcat support Ahmad Fatoum
  2024-10-16  8:56 ` [PATCH 2/2] string: support overriding strchr/strrchr/strstr macros Ahmad Fatoum
@ 2024-10-18  8:52 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-10-18  8:52 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Wed, 16 Oct 2024 10:56:07 +0200, Ahmad Fatoum wrote:
> We have a lot of kernel string functions already for use by common,
> driver and board code, but we are lacking strlcat.
> 
> Add it to simplify porting code using it.
> 
> 

Applied, thanks!

[1/2] string: add strlcat support
      https://git.pengutronix.de/cgit/barebox/commit/?id=7cb3478fc8a2 (link may not be stable)
[2/2] string: support overriding strchr/strrchr/strstr macros
      https://git.pengutronix.de/cgit/barebox/commit/?id=98154a75c83a (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




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

end of thread, other threads:[~2024-10-18  8:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-16  8:56 [PATCH 1/2] string: add strlcat support Ahmad Fatoum
2024-10-16  8:56 ` [PATCH 2/2] string: support overriding strchr/strrchr/strstr macros Ahmad Fatoum
2024-10-18  8:52 ` [PATCH 1/2] string: add strlcat support Sascha Hauer

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