Re: strncpy() fails to copy
- From: Tom St Denis <tom@xxxxxxx>
- Date: Tue, 14 Sep 2010 07:08:20 -0700 (PDT)
On Sep 14, 10:03 am, David Resnick <lndresn...@xxxxxxxxx> wrote:
On Sep 14, 9:57 am, Tom St Denis <t...@xxxxxxx> wrote:
On Sep 14, 9:52 am, David Resnick <lndresn...@xxxxxxxxx> wrote:
On Sep 14, 9:16 am, Tom St Denis <t...@xxxxxxx> wrote:
So as you correctly pointed out (and David Resnick got wrong) you need
to -1 the sizeof of the buffer.
Yep, oops. Don't use either much at this point. Still thing strncpy
is awful tho.
Since you still have to use -1 on strncat how is it any different than
dest[sizeof(dest)-1] = 0;
strncpy(dest, src, ...);
It's basically the same code at that point.
It'd be nice if all strn*() functions guaranteed that within n-bytes
of the dest there is a NUL and just truncate as required. But that
would require a bit of forethought on the standard C library designers
part.
Tom
Again, because strncpy has a design second flaw for general purpose
string usage, that it internally does a stupid memset that can be
QUITE costly if the target buffer is big and the string to be copied
is small.
In my current app I'm more worried about buffer overflows or corrupt
[uninitialized] strings than a few microseconds of performance.
But as I said you *could* just set the n-1'th byte to 0 and get the
same effect.
Tom
.
- Follow-Ups:
- Re: strncpy() fails to copy
- From: David Resnick
- Re: strncpy() fails to copy
- References:
- strncpy() fails to copy
- From: arnuld
- Re: strncpy() fails to copy
- From: Mark Bluemel
- Re: strncpy() fails to copy
- From: Tom St Denis
- Re: strncpy() fails to copy
- From: Ben Bacarisse
- Re: strncpy() fails to copy
- From: Tom St Denis
- Re: strncpy() fails to copy
- From: David Resnick
- Re: strncpy() fails to copy
- From: Tom St Denis
- Re: strncpy() fails to copy
- From: David Resnick
- strncpy() fails to copy
- Prev by Date: Re: strncpy() fails to copy
- Next by Date: Re: strncpy() fails to copy
- Previous by thread: Re: strncpy() fails to copy
- Next by thread: Re: strncpy() fails to copy
- Index(es):
Relevant Pages
|