Where is strnlen() ?

Joerg Sonnenberger joerg at britannica.bec.de
Fri Aug 13 05:18:35 PDT 2004


On Fri, Aug 13, 2004 at 11:18:49AM +0000, Thordur Ivar B. wrote:
> I agree but what I was thinking at the time if I'm reciving user input to a
> program wich uses strlen I might be vonerable to buffer overflow attacks (But
> that has been cleard up) and ofcourse in most cases you know the length of a
> string you are using (exept when you are dealing with user input, wich was the
> case in my porting effort.) And since I'm a pedant I think that interducing
> new non-standard functions is not an option so I think I will have to
> "turn-my-brain-on" as I mentioned in a previous post.

size_t
strnlen(const void *str, size_t len)
{
	const void *end = memchr(str, '\0', len);
	if (end == NULL)
		return -1; /* len ? */
	return (size_t)(end - str);
}

Or similiar in your code should do the trick.

Joerg

> 
> Anyways thanks for the replays.
> 
> -- 
> As far as the laws of mathematics refer to reality, they are not
> certain, and as far as they are certain, they do not refer to reality.
>                 -- Albert Einstein
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"


More information about the freebsd-hackers mailing list