linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* man 3 strncpy error
@ 2014-03-01 18:31 Rick Stanley
       [not found] ` <1393698707.4142.12.camel-rnTwVquHiKrxZjqEEZ1hU1aTQe2KTcn/@public.gmane.org>
  0 siblings, 1 reply; 2+ messages in thread
From: Rick Stanley @ 2014-03-01 18:31 UTC (permalink / raw)
  To: mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w; +Cc: linux-man-u79uwXL29TY76Z2rM5mHXA

On the current page for `man 3 strncpy` there is an error in the sample
for forced termination if the copy does not result in a proper
null-terminated string. (Appears in both "Man-pages, release 3.58", and
on http://man7.org/linux/man-pages/man3/strncpy.3.html )

Current:
        
        strncpy(buf, str, n);
        if (n > 0)
            buf[n - 1]= '\0';
        
If n == 4, then 4 bytes are copied, (buf[0] - buf[3]) but n - 1 or
buf[3] is then overwritten with the '\0'.  Should be buf[n] instead.

Corrected code:
        
        strncpy(buf, str, n);
        if (n > 0)
            buf[n]= '\0';
        
Question:  Even if n == 0 why shouldn't buf[n] be set to '\0'?

Thank you!

Rick Stanley

-- 
RSI (Rick Stanley, Inc.)
(917) 822-7771
www.rsiny.com
Computer Systems Consulting
Linux & Open Source Specialists



--
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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-03-04 16:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-01 18:31 man 3 strncpy error Rick Stanley
     [not found] ` <1393698707.4142.12.camel-rnTwVquHiKrxZjqEEZ1hU1aTQe2KTcn/@public.gmane.org>
2014-03-04 16:39   ` Michael Kerrisk (man-pages)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).