by rkmark · 6y ago · 0 comments
I begin to understand the people who write their own libc for security reasons.
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.> 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
Would this matter?
No, it is not true for almost all non-embedded systems. And char cannot be as wide as int, it is the opposite true, int can be as wide as char.