Avoid void* pointer arithmetic.
Convert int c to unsigned char, so c=<negative char value> will match.
Do not decrement pointer below start of array (even when value is not used).
}
#endif
-/*
+/*
* Memory Reverse Search
*/
void *
-lutil_memrchr(const void *b, int c, size_t n)
+lutil_memrchr(const void *b, int c, size_t n)
{
if (n != 0) {
- const unsigned char *s;
+ const unsigned char *s, *bb = b, cc = c;
- for ( s = b + n; s-- > b; ) {
- if ( *s == c ) {
- return s;
+ for ( s = bb + n; s > bb; ) {
+ if ( *--s == cc ) {
+ return (void *) s;
}
}
}
return NULL;
-}
-
+}