lib: sbi: Fix potential garbage data in string copy functions
PATCH 1/1 lib: sbi: Fix potential garbage data in string copy functions
From: Dongdong Zhang <[email protected]>
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.
--- lib/sbi/sbi_string.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
lib/sbi/sbi_string.c +8 -8
@@ -68,22 +68,22 @@ char *sbi_strcpy(char *dest, const char *src) {char *ret = dest; -while (*src !='\0' ) { - *dest++ = *src++; +while ((*dest++ = *src++) !='\0' ) { } -return ret; }char *sbi_strncpy (char *dest,const char *src,size_t count) { -char *ret = dest; +char *tmp = dest; -while (count-- && *src !='\0' ) { - *dest++ = *src++; +while (count) { +if ((*tmp = *src) !=0 ) + src++; + tmp++; + count--; } - -return ret; +return dest; }char *sbi_strchr (const char *s,int c)
Signature · 1 line
2.17.1
PATCH 1/1 lib: sbi: Fix potential garbage data in string copy functions
? 2025-02-19?? 09:57 +0800?zhangdongdong at eswincomputing.com???
Quoted 10 lines
From: Dongdong Zhang <[email protected]>
In the original implementation of
sbi_strcpyandsbi_strncpy, if the destination buffer (dest) was longer than the source string (src), the functions did not ensure that the remaining bytes indestwere 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 fromsrcwithout explicitly terminatingdest.
LGTM
Quoted 44 lines
Signed-off-by Dongdong Zhang <[email protected]>---
?lib/sbi/sbi_string.c | 16 ++++++++--------
?1 file changed, 8 insertions(+), 8 deletions(-)lib/sbi/sbi_string.c +0
-0@@ -68,22 +68,22 @@ char *sbi_strcpy(char *dest, const char *src) ?{ ? char *ret = dest;?
lib/sbi/sbi_string.c +1
-2-while (*src !='\0' ) { - *dest++ = *src++; +while ((*dest++ = *src++) !='\0' ) {? }
-
? return ret;
?}
?
?char *sbi_strncpy(char *dest, const char *src,size_tcount) ?{lib/sbi/sbi_string.c +1
-1-char *ret = dest; +char *tmp = dest;?
lib/sbi/sbi_string.c +5
-2-while (count-- && *src !='\0' ) { - *dest++ = *src++; +while (count) { +if ((*tmp = *src) !=0 ) + src++; + tmp++; + count--;? }
lib/sbi/sbi_string.c +1
-2- -return ret; +return dest;?}
?
?char *sbi_strchr(const char *s, int c)Signature · 1 line
2.17.1
Anup Patel
AppliedPATCH 1/1 lib: sbi: Fix potential garbage data in string copy functions
In reply to [email protected]Quoted 11 lines · On Wed, Feb 19, 2025 at 7:28?AM <[email protected]> wrote:
From: Dongdong Zhang <[email protected]>
In the original implementation of
sbi_strcpyandsbi_strncpy, if the destination buffer (dest) was longer than the source string (src), the functions did not ensure that the remaining bytes indestwere 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 fromsrcwithout explicitly terminatingdest.
Quoted 1 line
Signed-off-by Dongdong Zhang <[email protected]>
LGTM.
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 47 lines
--- lib/sbi/sbi_string.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)lib/sbi/sbi_string.c +8
-9@@ -68,22 +68,22 @@ char *sbi_strcpy(char *dest, const char *src) {char *ret = dest; -while (*src !='\0' ) { - *dest++ = *src++; +while ((*dest++ = *src++) !='\0' ) { } -return ret; }char *sbi_strncpy (char *dest,const char *src,size_t count) { -char *ret = dest; +char *tmp = dest; -while (count-- && *src !='\0' ) { - *dest++ = *src++; +while (count) { +if ((*tmp = *src) !=0 ) + src++; + tmp++; + count--; } - -return ret; +return dest; }char *sbi_strchr (const char *s,int c) --2.17.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi