diff --git a/ports/bare-arm/lib.c b/ports/bare-arm/lib.c index 61d3be64e3..1574d5c902 100644 --- a/ports/bare-arm/lib.c +++ b/ports/bare-arm/lib.c @@ -110,8 +110,9 @@ char *strchr(const char *s, int c) { } int strncmp(const char *s1, const char *s2, size_t n) { - while (*s1 && *s2 && n-- > 0) { + while (n > 0 && *s1 && *s2) { int c = *s1++ - *s2++; + --n; if (c) { return c; } diff --git a/shared/libc/string0.c b/shared/libc/string0.c index a3b268e441..3909f70ed8 100644 --- a/shared/libc/string0.c +++ b/shared/libc/string0.c @@ -154,7 +154,7 @@ int strcmp(const char *s1, const char *s2) { } int strncmp(const char *s1, const char *s2, size_t n) { - while (*s1 && *s2 && n > 0) { + while (n > 0 && *s1 && *s2) { char c1 = *s1++; // XXX UTF8 get char, next char char c2 = *s2++; // XXX UTF8 get char, next char n--;