summaryrefslogtreecommitdiff
path: root/lib/sbi/sbi_string.c
AgeCommit message (Collapse)Author
2026-04-06lib: Fix sbi_strchr to correctly handle null terminator searchChen Pei
The original sbi_strchr implementation did not conform to the C standard behavior. According to the C standard and POSIX specification, strchr(s, 0) should return a pointer to the null terminator at the end of string s. The previous implementation used a while loop that would terminate when either reaching the end of string or finding the character, but it would return NULL when searching for the null terminator instead of returning a pointer to the null terminator itself. The fixed implementation uses a do-while loop that ensures even when searching for the null terminator, the function correctly returns a pointer to the null terminator position rather than NULL. This fix ensures sbi_strchr behavior aligns with standard library function semantics, making it more predictable and safe for users expecting standard C library behavior. Signed-off-by: Chen Pei <[email protected]> Reviewed-by: Anup Patel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Anup Patel <[email protected]>
2025-03-23lib: sbi: Fix potential garbage data in string copy functionsDongdong Zhang
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 <[email protected]> Reviewed-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-09-13lib: sbi: Fix sbi_strnlen wrong count decrementRahul Pathak
count(maxlen) should not be decremented here Fixes: 1901e8a287bc ("platform: Add minimal libc support.") Signed-off-by: Rahul Pathak <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2022-01-21lib: fix pointer of type 'void *' used in arithmeticJukka Laitinen
Using "void *" in arithmetic causes errors with strict compiler settings: "error: pointer of type 'void *' used in arithmetic [-Werror=pointer-arith]" Avoid these by calculating on "char *" where 1-byte data size is assumed. Signed-off-by: Jukka Laitinen <[email protected]> Reviewed-by: Dong Du <[email protected]> Reviewed-by: Xiang W <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2021-12-23firmware: Move memcpy/memset mapping to fw_base.SAnup Patel
Some of the external firmwares using OpenSBI as library are facing issues with the weak memcpy() and memset() aliases in libsbi.a so we move these to fw_base.S. This way mapping of implicit memcpy() or memset() calls to sbi_memcpy() or sbi_memset() will only be done for OpenSBI firmwares. (Refer, https://github.com/riscv-software-src/opensbi/issues/234) In addition, we also add memmove() and memcmp() mappings in fw_base.S because as-per the GCC documentation the freestanding environment must provide memcpy(), memmove(), memset(), and memcmp(). Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Atish Patra <[email protected]>
2021-12-11lib: sbi: Fix compile errors using -Os optionAnup Patel
When compiling with -Os option along with -ffreestanding, both GCC and clang will add implicit calls to memcpy() and memset() for stack variables initialized in declaration. The C standard as per Clause 4, the compiler cannot necessarily assume that anything beyond: * float.h * iso646.h * limits.h * stdalign.h * stdarg.h * stdbool.h * stddef.h * stdint.h * stdnoreturn.h * fenv.h * math.h * and the numeric conversion functions of stdlib.h. This patch maps memcpy() and memset() as weak-alias of sbi_memcpy() and sbi_memset() respectively so that implicit calls to memcpy() and memset() will compile properly. Signed-off-by: Anup Patel <[email protected]> Reviewed-by: Dong Du <[email protected]> Reviewed-by: Xiang W <[email protected]>
2021-08-07lib: sbi: Fix bug in strncmp function when count is 0Dong Du
No need to compare characters when the count turns to 0. Fix the issue in sbi_strncmp. Signed-off-by: Dong Du <[email protected]> Reviewed-by: Bin Meng <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2020-07-29lib: Add sbi_strncmp implementationAbner Chang
This commit add an implementation of sbi_strncmp. Signed-off-by: Abner Chang <[email protected]> Reviewed-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]>
2019-06-19lib: Rename string.x to sbi_string.xAtish Patra
All string functions are part of libsbi. It makes more sense to rename them to sbi_string.x as the libsbi can be linked with external libraries that can have similar implementation. Signed-off-by: Atish Patra <[email protected]> Acked-by: Anup Patel <[email protected]>