apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Trawick <traw...@attglobal.net>
Subject Re: apr_pstrndup
Date Wed, 28 Nov 2001 13:22:10 GMT
Brian Pane <bpane@pacbell.net> writes:

> Doug MacEachern wrote:
> 
> >does this:
> >    end = memchr(s, '\0', n);
> >    if (end != NULL)
> >        n = end - s;
> >
> >is that just  to avoid allocating an extra byte if 's' already contains
> >'\0' at the end?  seem like it would be better to waste the extra byte
> >than to scan the whole string with memchr() or at least change that to:
> >    if (*(s + (n-1)) == '\0') {
> >        n--;
> >    }
> >
> 
> There's no guarantee that strlen(s) is anywhere near n.  It's valid
> for a caller to do this, for example:
>   apr_pstrndup(p, "foo", 65536);

It looks to me that in practically all calls to apr_pstrndup() in the
httpd+apr+apr-util source tree the caller has told apr_pstrndup() how
many characters are in the string to duplicate, such that there is no
need to look for any '\0'.  The callers want to take the first n bytes
of a longer string and put them in their own complete string.

Clearly we could use an API where the caller says "take these n bytes
of chars, and stick those n bytes + a terminating '\0' in a buffer
allocated from this pool, with minimal overhead."

Whether or not we should change the semantics of the existing function
or use a different name is subject to consideration :)

-- 
Jeff Trawick | trawick@attglobal.net | PGP public key at web site:
       http://www.geocities.com/SiliconValley/Park/9289/
             Born in Roswell... married an alien...

Mime
View raw message