[parted-devel] [PATCH 84/86] Fix the length of several strncpy calls

Brian C. Lane bcl at redhat.com
Mon Aug 13 23:50:30 BST 2018


On Wed, Jul 25, 2018 at 09:04:28AM -0400, Phillip Susi wrote:
> On 7/24/2018 7:04 PM, Brian C. Lane wrote:
> >> strncpy doesn't fill the remainder with zeros, and if it runs out of
> >> space, leaves the string unterminated, so you have to manually set the
> >> last byte of the buffer to zero.
> > 
> > Not according to the manpage for strncpy on Fedora 28:
> > 
> > "If the length of src is less than n, strncpy() writes additional null
> > bytes to dest to ensure that a total of n bytes are written."
> 
> Ahh, missed that part.  But the important part is this:
> 
> Warning: If there is no null byte among the first n bytes of src, the
> string placed in dest will not  be  null-terminated.

https://sourceware.org/git/?p=glibc.git;a=blob;f=string/strncpy.c;h=bb2abe30e59ee633b7aeec2846093f57afd0d17c;hb=HEAD

There's only a problem if they are the same length. But after looking at
how it uses n I realize that yes, that's still a problem because it uses
that as the length of the destination and never fills the whole thing.

So I'll go through things and make sure the last byte is always getting
a 0x00 stuffed into it.

Thanks!

-- 
Brian C. Lane (PST8PDT)



More information about the parted-devel mailing list