[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: strlcpy and strlcat in linux libc ?

Richard Braakman <dark@xs4all.nl> writes:

> On Mon, Mar 04, 2002 at 04:11:25PM -0800, Joseph Carter wrote:
> > So say the glibc people in the threads referenced.  If the implementation
> > is flawed, that should be fixed.  The preconditions, postconditions, and
> > function declaration are a fine improvement, however.
> Are these stated anywhere?  I didn't read the mail archives referenced
> at the beginning of this thread, I'm using my memory of a disussion
> on glibc-dev (and cross-posted to hell and back :).  One of the reasons
> I saw for not including these functions was that they were underspecified,
> and the specification (i.e. manpage) would have to be deduced from the code,
> which wasn't the same in all implementations.

I can't agree with that after I read the manpage at [1].
> > Also, notice that strncpy returns a pointer to dest.  This is almost never
> > used by anyone.  I personally have never used it actually.  strlcpy does
> > return a value, but returns instead the number of characters copied from
> > src.  This too I use rarely, but whereas strncpy's return value has never
> > been even remotely useful to me, strlcpy's has upon occasion.
> ... and strlcat returns the sum of the lengths of the strings, even if
> that didn't fit in the destination buffer.  That's why the function was
> called inefficient -- the implementation has to scan the entire source
> string, even if it doesn't have to copy all of it, for the sake of a
> rarely used return value.

That's wrong.  The strlc* functions return the length of the string
that was created (even though the manpage [1] uses the mistakable
wording ``tried to create'' in the return values section it's obvious
from the rest of the document).  So you can use the return value of
strlc* instead of calling strlen afterwards.

There is also an exhaustive text [2] from Todd C. Miller and Theo de
Raadt explaining the strlc* functions and how they compare to their
strc* and strnc* counterparts.

[1] http://www.tac.eu.org/cgi-bin/man-cgi?strlcat+3
[2] http://www.usenix.org/events/usenix99/full_papers/millert/millert_html/

The most important vi key sequence: ESC q !
  exit and save the file -- from being corrupted.

Reply to: