Skip to content

Commit 5d186d1

Browse files
committed
Yet another fix to strcmp.
I swear I know how to code.
1 parent a6d233c commit 5d186d1

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

libc/include/string.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,20 @@ static __inline__ char * strncat(char * s1, const char * s2, size_t n) {
153153
}
154154

155155
static __inline__ int strcmp(const char * s1, const char * s2) {
156-
char c1, c2;
157-
while ((c1 = *s1++)) {
158-
if ((c2 = *s2++) != c1) break;
156+
int c1, c2;
157+
while (1) {
158+
if (((c1 = *s1++) != (c2 = *s2++)) || !c1 || !c2)
159+
break;
159160
}
160161
return c1 - c2;
161162
}
162163

163164
static __inline__ int strncmp(const char * s1, const char * s2, size_t n) {
164-
char c1, c2;
165-
while (n--)
166-
if ((c1 = *s1++) != (c2 = *s2++))
165+
int c1, c2;
166+
while (n--) {
167+
if (((c1 = *s1++) != (c2 = *s2++)) || !c1 || !c2)
167168
return c1 - c2;
169+
}
168170

169171
return 0;
170172
}

0 commit comments

Comments
 (0)