[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: More than you necessarily wanted to know about tor_strndup performance improvements from late-2004 [was Re: tor callgrinds]



On Sun, Feb 18, 2007 at 10:19:15AM -0500, Watson Ladd wrote:
> It provides the length of the source string, not the destination string.
> This lets you compute the space you need, but is slower. I made the same
> mistake.

You do not compute the dest space by calling strlcpy(). You know the space
ahead of time.

> > 	return(s - src);	/* count does not include NUL */
> > }
> > 
> > Provided the string 'onion\0routing\0', with n being 13, only 6 loop
> > cycles will actually happen.
> No, OpenBSD strlcpy is different. It returns strlen(src) which Tor copy
> doesn't.

No it isn't really significantly different. This is right out of CVS:
http://www.openbsd.org/cgi-bin/cvsweb/~checkout~/src/lib/libc/string/strlcpy.c?rev=1.11&content-type=text/plain

size_t
strlcpy(char *dst, const char *src, size_t siz)
{
	char *d = dst;
	const char *s = src;
	size_t n = siz;

	/* Copy as many bytes as will fit */
	if (n != 0) {
		while (--n != 0) {
			if ((*d++ = *s++) == '\0')
				break;
		}
	}

	/* Not enough room in dst, add NUL and traverse rest of src */
	if (n == 0) {
		if (siz != 0)
			*d = '\0';		/* NUL-terminate dst */
		while (*s++)
			;
	}

	return(s - src - 1);	/* count does not include NUL */
}

It's only minorly varied from the 1998 version tor is using.

Also, please truncate your replies to not include the entire quoted material
if you're only referring to one section.

-cl