[quagga-dev 3087] Re: strlcpy implementation correct?

Andrew J. Schorr aschorr at telemetry-investments.com
Sat Apr 2 18:29:22 BST 2005

On Sat, Apr 02, 2005 at 08:01:49PM +0300, Hasso Tepper wrote:
> Sure, look at toplevel comment in str.c. Replacing these have been in my 
> todo list, but haven't time for it yet. I have always something more 
> important todo ;). Feel free to replace these with any GPL'ed 
> strlcat/stlcpy implementations. 

Yes, the comment is infuriating.  Why would somebody bother to implement
functions that do not work correctly?  This causes bugs in the code.

Will this work for now?  It should be optimized to avoid reading through
the src string twice, but that's a bit more complicated...

strlcpy(char *dst, const char *src, size_t size)
  /* N.B. This implementation is very inefficient. */
  strncpy(dst, src, size);

  /* Make sure it terminates with a NUL char. */
  if (size > 0)
    dst[size-1] = '\0';

  return strlen(src);

> Btw, Linux is the only platform which will use these. All other supported 
> platforms (*BSD and Solaris) have strlcpy and strlcat in libc. Many 
> projects have their own implementations in their libs because of that.

Yes, openssh has it, and so does rsync.  Rsync is GPL, so could take
from there.  I'm not sure it's the greatest implementation...


More information about the Quagga-dev mailing list