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

Mike Fleetwood mike.fleetwood at googlemail.com
Wed Jul 25 08:05:50 BST 2018


On 25 July 2018 at 00:04, Brian C. Lane <bcl at redhat.com> wrote:
> On Tue, Jul 24, 2018 at 09:15:44AM -0400, Phillip Susi wrote:
>> On 7/23/2018 8:26 PM, Brian C. Lane wrote:
>> > These need to be 1 less than the allocated size of the buffer, strncpy
>> > will fill the remainder with zeros, but there needs to be room for at
>> > least one 0x00 at the end.
>>
>> 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."

The concerning case is as described in the previous sentence in the
manpage:
"Warning: If there is no null byte among the first n bytes of src, the
string placed in dest will not be null-terminated."

Mike



More information about the parted-devel mailing list