[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: crusade against strncpy/strcpy ;-)
From: |
Pádraig Brady |
Subject: |
Re: crusade against strncpy/strcpy ;-) |
Date: |
Thu, 19 Apr 2012 12:49:14 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:6.0) Gecko/20110816 Thunderbird/6.0 |
On 04/19/2012 12:15 PM, Jim Meyering wrote:
> strncpy is particularly bad, but even strcpy is best avoided.
Yep, strncpy may not NUL terminate,
so using it is awkward as detailed here:
http://www.pixelbeat.org/programming/gcc/string_buffers.html
stpcpy/stpncpy are appropriate here as the buffer is just
the right size, so you don't waste cycles filling with NULs etc.
Often it's simpler/faster to fall back to memcpy for C string buffers
as I did in buffer_or_output() here:
http://git.sv.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=b1428e31
patch looks good.
cheers,
Pádraig.