Loading ...
Sorry, an error occurred while loading the content.

Re: vim_strncpy() behaviour surprising

Expand Messages
  • Bram Moolenaar
    ... Right, the manpage of strncpy() does mention the padding with NUL bytes. I ve always considered the design of strncpy() wrong. There is no reason to pad
    Message 1 of 5 , Feb 5, 2003
    • 0 Attachment
      Daniel Elstner wrote:

      > On Die, 2003-02-04 at 23:15, Bram Moolenaar wrote:
      >
      > > 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.


      Right, the manpage of strncpy() does mention the padding with NUL bytes.

      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 safe
      > to make vim_strncpy() not do this. On the other hand, people writing
      > patches for Vim might not be aware of this subtlety...

      I don't see why someone would need to rely on vim_strncpy() doing
      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 ///
    • Mike Williams
      ... Like a lot of things, not to break old C code. I remember stories of being used to pad Unix directory entries to a fixed size. Ah, the following may be
      Message 2 of 5 , Feb 5, 2003
      • 0 Attachment
        On 5 Feb 2003 at 14:21, Bram Moolenaar wrote:

        > Right, the manpage of strncpy() does mention the padding with NUL bytes.
        >
        > 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.

        Like a lot of things, not to break old C code. I remember stories of
        being used to pad Unix directory entries to a fixed size. Ah, the
        following may be of interest in C programming trivia:

        http://groups.google.com/groups?q=strncpy+nul+pad&hl=en&lr=&ie=UTF-
        8&selm=DHJBAp.5p1%40eskimo.com&rnum=4

        (Yep, it's wrapped - sorry).

        > I don't see why someone would need to rely on vim_strncpy() doing
        > padding. I'll add a note about this in the comments to avoid
        > confusion.

        Hmm, may be better to alter the name as the old timers may just
        think, ah, strncpy() - does nul padding. How about
        vim_strncpy_nopad()? A bit cumbersome I know.

        TTFN

        Mike
        --
        The search for happiness is one of the chief sources of unhappiness.
      Your message has been successfully submitted and would be delivered to recipients shortly.