From 9cdab51feba51881b55684d6f29b956ad6e117a4 Mon Sep 17 00:00:00 2001 From: Md Sadre Alam Date: Thu, 26 Apr 2018 16:09:45 +0530 Subject: [PATCH 1/2] lib: string: Add strlcat() and memscpy() Add strlcat() and memscpy() currently its not present in uboot. make these function available in uboot. Update function declaration also in headers as avoid warnings. strlcat(): Append a length limited string to another. memscpy(): Copy data between buffer. The main aim of memscpy is to prevent buffer overflows by taking in both the destination size and copy size. Change-Id: Icfc4cb4f8d668c0cace02af97617ccc3eecc5651 Signed-off-by: Md Sadre Alam --- include/linux/string.h | 3 +++ lib/string.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index c7047ba0bc..8d168e0301 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -39,6 +39,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 size_t strlcat(char *, const char *, __kernel_size_t); +#endif #ifndef __HAVE_ARCH_STRCMP extern int strcmp(const char *,const char *); #endif diff --git a/lib/string.c b/lib/string.c index 87c9a408e6..0dec46ccad 100644 --- a/lib/string.c +++ b/lib/string.c @@ -175,6 +175,34 @@ char * strncat(char *dest, const char *src, size_t count) } #endif +#ifndef __HAVE_ARCH_STRLCAT +/** + * strlcat - Append a length-limited, %NUL-terminated string to another + * @dest: The string to be appended to + * @src: The string to append to it + * @count: The size of the destination buffer. + * + */ +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; + memcpy(dest, src, len); + dest[len] = 0; + return res; +} +EXPORT_SYMBOL(strlcat); +#endif + #ifndef __HAVE_ARCH_STRCMP /** * strcmp - Compare two strings @@ -520,6 +548,12 @@ void * memcpy(void *dest, const void *src, size_t count) } #endif +size_t memscpy(void *dest, size_t dst_size, const void *src, size_t copy_size) { + size_t min_size = dst_size < copy_size ? dst_size : copy_size; + memcpy(dest, src, min_size); + return min_size; +} + #ifndef __HAVE_ARCH_MEMMOVE /** * memmove - Copy one area of memory to another From 94028baeb5bdcf716b37f1b84fdc8e4cdb6a6e22 Mon Sep 17 00:00:00 2001 From: Md Sadre Alam Date: Thu, 26 Apr 2018 16:51:43 +0530 Subject: [PATCH 2/2] lib: string: compile strlcat() and memscpy() Make strlcat() and memscpy() function as compilable by removing unnecessary things. Change-Id: I77759fbf7e150a169d9a756542c806493e85e183 Signed-off-by: Md Sadre Alam --- include/linux/string.h | 3 +++ lib/string.c | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 8d168e0301..f7284452ee 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -83,6 +83,9 @@ extern void * memset(void *,int,__kernel_size_t); #ifndef __HAVE_ARCH_MEMCPY extern void * memcpy(void *,const void *,__kernel_size_t); #endif +#ifndef __HAVE_ARCH_MEMSCPY +extern size_t memscpy(void *dest, size_t dst_size, const void *src, size_t copy_size); +#endif #ifndef __HAVE_ARCH_MEMMOVE extern void * memmove(void *,const void *,__kernel_size_t); #endif diff --git a/lib/string.c b/lib/string.c index 0dec46ccad..233adeeec3 100644 --- a/lib/string.c +++ b/lib/string.c @@ -200,7 +200,6 @@ size_t strlcat(char *dest, const char *src, size_t count) dest[len] = 0; return res; } -EXPORT_SYMBOL(strlcat); #endif #ifndef __HAVE_ARCH_STRCMP @@ -548,11 +547,30 @@ void * memcpy(void *dest, const void *src, size_t count) } #endif -size_t memscpy(void *dest, size_t dst_size, const void *src, size_t copy_size) { +#ifndef __HAVE_ARCH_MEMSCPY +/** + * memscpy - Copy one area of memory to another + * @dest: Where to copy to + * @src: Where to copy from + * @dst_size: The size of destination buffer area + * @copy_size: The size of source buffer area + * + * You should not use this function where memcpy + * was used to copy null terminated buffer, the + * replacement function is strlcpy() and strlcat() + * depending on situation. + * + * The aim of memscpy() is to prevent buffer overflow + * by taking both destination buffer size and source + * buffer size. + */ +size_t memscpy(void *dest, size_t dst_size, const void *src, size_t copy_size) +{ size_t min_size = dst_size < copy_size ? dst_size : copy_size; memcpy(dest, src, min_size); return min_size; } +#endif #ifndef __HAVE_ARCH_MEMMOVE /**