Skip to contentSkip to conversation

Add string test to SBI unit test suite

PATCH Applied [email protected] 2 participants 4 messages Mar 6 – Apr 6

lore
Share & tools
lore
#0 lore raw

cover Add string test to SBI unit test suite

From: Chen Pei <[email protected]>

The string tests were added to the SBI SBI unit test framework; then, it was discovered that the implementation of sbi_strchr was not aligned with the C standard behavior, which was fixed in the second patch.

Chen Pei (2):
lib: tests: Add test for string
lib: Fix sbi_strchr to correctly handle null terminator search

 lib/sbi/sbi_string.c            |  11 +-
 lib/sbi/tests/objects.mk        |   3 +
 lib/sbi/tests/sbi_string_test.c | 372 ++++++++++++++++++++++++++++++++
 3 files changed, 380 insertions(+), 6 deletions(-)
 create mode 100644 lib/sbi/tests/sbi_string_test.c
Signature · 7 lines
2.50.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw

PATCH 1/2 lib: tests: Add test for string

From: Chen Pei <[email protected]>

Added unit tests for various string operations using SBI unit test framework.

---
 lib/sbi/tests/objects.mk        |   3 +
 lib/sbi/tests/sbi_string_test.c | 372 ++++++++++++++++++++++++++++++++
 2 files changed, 375 insertions(+)
 create mode 100644 lib/sbi/tests/sbi_string_test.c
lib/sbi/tests/objects.mk +3 -0
index 8da839f6..3ee1c635 100644
@@ -21,3 +21,6 @@ libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_ecall_test.o
 
 carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += bitops_test_suite
 libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_bitops_test.o
+
+carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += string_test_suite
+libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_string_test.o
lib/sbi/tests/sbi_string_test.c new file +372 -0

372 lines not on this page · raw message

Signature · 7 lines
2.50.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw

PATCH 2/2 lib: Fix sbi_strchr to correctly handle null terminator search

In reply to [email protected]

From: Chen Pei <[email protected]>

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.

---
 lib/sbi/sbi_string.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
lib/sbi/sbi_string.c +5 -6
index f4f13942..3a10c254 100644
@@ -88,13 +88,12 @@ char *sbi_strncpy(char *dest, const char *src, size_t count)
 
 char *sbi_strchr(const char *s, int c)
 {
-	while (*s != '\0' && *s != (char)c)
-		s++;
+	do {
+		if (*s == (char)c)
+			return (char *)s;
+	} while (*s++ != '\0');
 
-	if (*s == '\0')
-		return NULL;
-	else
-		return (char *)s;
+	return NULL;
 }
 
 char *sbi_strrchr(const char *s, int c)
Signature · 7 lines
2.50.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on the v1 series Applied
#3 lore raw
In reply to [email protected]
Quoted 11 lines · On Fri, Mar 6, 2026 at 3:14 PM <[email protected]> wrote:

From: Chen Pei <[email protected]>

The string tests were added to the SBI SBI unit test framework; then, it was discovered that the implementation of sbi_strchr was not aligned with the C standard behavior, which was fixed in the second patch.

Chen Pei (2):
lib: tests: Add test for string
lib: Fix sbi_strchr to correctly handle null terminator search

Quoted 6 lines
 lib/sbi/sbi_string.c            |  11 +-
 lib/sbi/tests/objects.mk        |   3 +
 lib/sbi/tests/sbi_string_test.c | 372 ++++++++++++++++++++++++++++++++
 3 files changed, 380 insertions(+), 6 deletions(-)
 create mode 100644 lib/sbi/tests/sbi_string_test.c

LGTM.

Applied this series to the riscv/opensbi repo.

Thanks,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi