By using a for loop instead of a while loop, the pointers s1 and s2 are not unnecessarily incremented, and how they're manipulated is not all over the place.

Index: strcmp.c
===================================================================
RCS file: /cvs/src/lib/libc/string/strcmp.c,v
retrieving revision 1.9
diff -u -p -r1.9 strcmp.c
--- strcmp.c    31 Aug 2015 02:53:57 -0000      1.9
+++ strcmp.c    11 Sep 2020 01:33:09 -0000
@@ -40,9 +40,9 @@
 int
 strcmp(const char *s1, const char *s2)
 {
-       while (*s1 == *s2++)
-               if (*s1++ == 0)
+       for (; *s1 == *s2; s1++, s2++)
+               if (*s1 == 0)
                        return (0);
-       return (*(unsigned char *)s1 - *(unsigned char *)--s2);
+       return (*(unsigned char *)s1 - *(unsigned char *)s2);
 }
 DEF_STRONG(strcmp);
Index: strcmp.c
===================================================================
RCS file: /cvs/src/lib/libc/string/strcmp.c,v
retrieving revision 1.9
diff -u -p -u -p -r1.9 strcmp.c
--- strcmp.c    31 Aug 2015 02:53:57 -0000      1.9
+++ strcmp.c    11 Sep 2020 01:55:15 -0000
@@ -40,9 +40,9 @@
 int
 strcmp(const char *s1, const char *s2)
 {
-       while (*s1 == *s2++)
-               if (*s1++ == 0)
+       for (; *s1 == *s2; s1++, s2++)
+               if (*s1 == 0)
                        return (0);
-       return (*(unsigned char *)s1 - *(unsigned char *)--s2);
+       return (*(unsigned char *)s1 - *(unsigned char *)s2);
 }
 DEF_STRONG(strcmp);

Reply via email to