[klibc] [klibc:master] strrchr: Handle c == 0 correctly

klibc-bot for Ben Hutchings ben at decadent.org.uk
Fri Dec 30 14:03:12 PST 2022


Commit-ID:  61d2ea539c88f7862b3992b9a00daaedb6bb68ef
Gitweb:     http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=61d2ea539c88f7862b3992b9a00daaedb6bb68ef
Author:     Ben Hutchings <ben at decadent.org.uk>
AuthorDate: Fri, 30 Dec 2022 22:48:55 +0100
Committer:  Ben Hutchings <ben at decadent.org.uk>
CommitDate: Fri, 30 Dec 2022 23:00:38 +0100

[klibc] strrchr: Handle c == 0 correctly

strrchr(s, 0) should return a pointer to the null terminator,
not NULL.  Move the null terminator check to make this work.

Reported-by: Bugs Reporter <bugs at compiler.ai>
Signed-off-by: Ben Hutchings <ben at decadent.org.uk>

---
 usr/klibc/strrchr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/usr/klibc/strrchr.c b/usr/klibc/strrchr.c
index e36d37ee..af68ffe0 100644
--- a/usr/klibc/strrchr.c
+++ b/usr/klibc/strrchr.c
@@ -9,9 +9,11 @@ char *strrchr(const char *s, int c)
 {
 	const char *found = NULL;
 
-	while (*s) {
+	for (;;) {
 		if (*s == (char)c)
 			found = s;
+		if (!*s)
+			break;
 		s++;
 	}
 


More information about the klibc mailing list