Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Aug 2004 14:17:11 +0200
From:      Joerg Sonnenberger <joerg@britannica.bec.de>
To:        freebsd-hackers@freebsd.org
Subject:   Re: Where is strnlen() ?
Message-ID:  <20040813121711.GC839@britannica.bec.de>
In-Reply-To: <20040813111849.047fae64.thib@mi.is>
References:  <20040811193254.6f0be2c2.thib@mi.is> <20040811200323.GA37059@xor.obsecurity.org> <20040811203832.728c915b.thib@mi.is> <20040812231519.GB7173@gothmog.gr> <20040813111849.047fae64.thib@mi.is>

next in thread | previous in thread | raw e-mail | index | archive | help
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@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040813121711.GC839>