Well, on one hand, writing correct albeit slow memcmp() is easy, on the other hand, it has some gotchas...
int my_memcmp(const void * ptr1, const void * ptr2, size_t num) {
const unsigned char * p1 = ptr1;
const unsigned char * p2 = ptr2;
for (size_t i = 0; i < num; i++) {
if p1[i] < p2[i] {
return -1;
}
if p1[i] > p2[i] {
return 1;
}
}
return 0;
}
For example, technically speaking, unsigned char can be as wide as an int, so "p1[i] - p2[i]" may actually evaluate to unsigned int which is not what you want.
I begin to understand the people who write their own libc for security reasons.