Re: vim_strncpy() behaviour surprising
- Daniel Elstner wrote:
> On Die, 2003-02-04 at 23:15, Bram Moolenaar wrote:Right, the manpage of strncpy() does mention the padding with NUL bytes.
> > Why would strncpy() pad with NUL bytes? Perhaps this is a debug
> > version?
> It's the standard, documented behaviour of strncpy(). From the manpage:
> In the case where the length of src is less than that of
> n, the remainder of dest will be padded with nulls.
I've always considered the design of strncpy() wrong. There is no
reason to pad with NUL bytes, while it's essential to terminate a string
with a NUL even when it doesn't fit in the buffer.
> I agree though that if Vim doesn't expect the padding anyway it's safeI don't see why someone would need to rely on vim_strncpy() doing
> to make vim_strncpy() not do this. On the other hand, people writing
> patches for Vim might not be aware of this subtlety...
padding. I'll add a note about this in the comments to avoid confusion.
hundred-and-one symptoms of being an internet addict:
127. You bring your laptop and cellular phone to church.
/// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
/// Creator of Vim - Vi IMproved -- http://www.Vim.org \\\
\\\ Project leader for A-A-P -- http://www.A-A-P.org ///
\\\ Help AIDS victims, buy at Amazon -- http://ICCF.nl/click1.html ///
- On 5 Feb 2003 at 14:21, Bram Moolenaar wrote:
> Right, the manpage of strncpy() does mention the padding with NUL bytes.Like a lot of things, not to break old C code. I remember stories of
> I've always considered the design of strncpy() wrong. There is no
> reason to pad with NUL bytes, while it's essential to terminate a string
> with a NUL even when it doesn't fit in the buffer.
being used to pad Unix directory entries to a fixed size. Ah, the
following may be of interest in C programming trivia:
(Yep, it's wrapped - sorry).
> I don't see why someone would need to rely on vim_strncpy() doingHmm, may be better to alter the name as the old timers may just
> padding. I'll add a note about this in the comments to avoid
think, ah, strncpy() - does nul padding. How about
vim_strncpy_nopad()? A bit cumbersome I know.
The search for happiness is one of the chief sources of unhappiness.