(unknown)

2 messages, 2 authors, 2010-06-21 · open the first message on its own page

(unknown)

From: Bob Eggers <hidden>
Date: 2010-06-21 20:30:20

I noticed an error in and example on the man page for strncpy(). The page is http://www.kernel.org/doc/man-pages/online/pages/man3/strncpy.3.html

The error is in the section, "NOTES", specifically in the code example after the text:

      If there is no terminating null byte in the first n characters of src,
       strncpy() produces an unterminated string in dest.  Programmers often prevent
       this mistake by forcing termination as follows:

           strncpy(buf, str, n);
           if (n > 0)
               buf[n - 1]= '\0';

Substituting 1 for n and "123" for str, this would equate to:

           strncpy(buf, "123", 1);  // buf is now "1"
           if (1 > 0)
               buf[1 - 1]= '\0';                // buf is now ""

Bob Eggers--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Re:

From: bill o gallmeister <hidden>
Date: 2010-06-21 21:05:53

I believe that's the correct result for a NULL-terminated string of length 1.

- bog

On 06/21/2010 01:30 PM, Bob Eggers wrote:
I noticed an error in and example on the man page for strncpy(). The page is http://www.kernel.org/doc/man-pages/online/pages/man3/strncpy.3.html

The error is in the section, "NOTES", specifically in the code example after the text:

      If there is no terminating null byte in the first n characters of src,
       strncpy() produces an unterminated string in dest.  Programmers often prevent
       this mistake by forcing termination as follows:

           strncpy(buf, str, n);
           if (n > 0)
               buf[n - 1]= '\0';

Substituting 1 for n and "123" for str, this would equate to:

           strncpy(buf, "123", 1);  // buf is now "1"
           if (1 > 0)
               buf[1 - 1]= '\0';                // buf is now ""

Bob Eggers--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

-- 
Bill O. Gallmeister
bgallmeister-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
http://geekwhisperer.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help