Is the implementation of strnlen
that follows invalid?
size_t strnlen(const char *str, size_t maxlen)
{
char *nul = memchr(str, '\0', maxlen);
return nul ? (size_t)(nul - str) : maxlen;
}
I assume that memchr
may always look at maxlen
bytes no matter the contents of those bytes. Does the contract of strnlen
only allow it to look at all maxlen
bytes if there is no NUL terminator? If so, the size in memory of str
may be less than maxlen
bytes, in which case memchr
might try to read invalid memory locations. Is this correct?