From 0ab2f59fe822b62620018a4fc4c57d0e4b7b4396 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sun, 28 Apr 2024 19:35:12 -0700 Subject: [PATCH] [libc] Fix a strncmp bug The strncmp implementation too eagerly advanced the string pointers, such that it would compare the next characters after the ones that didn't match. --- src/libraries/libc/string/strncmp.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/libc/string/strncmp.cpp b/src/libraries/libc/string/strncmp.cpp index 10ddb70..67382cd 100644 --- a/src/libraries/libc/string/strncmp.cpp +++ b/src/libraries/libc/string/strncmp.cpp @@ -16,7 +16,9 @@ int strncmp(const char *s1, const char *s2, size_t n) { char const * c1 = s1; char const * c2 = s2; - while (n && *c1 && *c2 && *c1++ == *c2++) n--; + while (n && *c1 && *c2 && *c1 == *c2) { + n--; c1++; c2++; + } if (!n || *c1 == *c2) return 0; if (!*c2 || *c1 > *c2) return 1;