lib: sbi: Fix potential garbage data in string copy functions

In the original implementation of `sbi_strcpy` and `sbi_strncpy`, if the
destination buffer (`dest`) was longer than the source string (`src`),
the functions did not ensure that the remaining bytes in `dest` were
properly null-terminated. This could result in garbage data being
present in the destination buffer after the copy operation, as the
functions only copied characters from `src` without explicitly
terminating `dest`.

Signed-off-by: Dongdong Zhang <zhangdongdong@eswincomputing.com>
Reviewed-by: Xiang W <wxjstz@126.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
Dongdong Zhang
2025-02-19 09:57:26 +08:00
committed by Anup Patel
parent 0b78665a6c
commit 56341e95ae

View File

@@ -68,22 +68,22 @@ char *sbi_strcpy(char *dest, const char *src)
{ {
char *ret = dest; char *ret = dest;
while (*src != '\0') { while ((*dest++ = *src++) != '\0') {
*dest++ = *src++;
} }
return ret; return ret;
} }
char *sbi_strncpy(char *dest, const char *src, size_t count) char *sbi_strncpy(char *dest, const char *src, size_t count)
{ {
char *ret = dest; char *tmp = dest;
while (count-- && *src != '\0') { while (count) {
*dest++ = *src++; if ((*tmp = *src) != 0)
src++;
tmp++;
count--;
} }
return dest;
return ret;
} }
char *sbi_strchr(const char *s, int c) char *sbi_strchr(const char *s, int c)