[ipxe-devel] [PATCH] [efi] use correct bound in strncpy to ensure NUL-termination

Bruce Rogers brogers at suse.com
Mon Apr 23 19:17:59 UTC 2018


>>> On 4/23/2018 at 1:04 PM, Thomas Miletich <thomas.miletich at gmail.com> wrote:
> On Mon, Apr 23, 2018 at 7:53 PM, Michael Brown <mcb30 at ipxe.org> wrote:
>> On 23/04/18 16:42, Bruce Rogers wrote:
>>>
>>> Using gcc8 with the [-Werror=stringop-truncation] option, the following
>>> error is emitted:
>>>
>>> util/elf2efi.c:494:2: error: 'strncpy' specified bound 8 equals
>>> destination
>>> size [-Werror=stringop-truncation]
>>>    strncpy ( ( char * ) new->hdr.Name, name, sizeof ( new->hdr.Name ) );
>>>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>>
>>> Specify one less than sizeof the target buffer to avoid this diagnostic.
>>> Since the target buffer is pre-zeroed, the string will be NUL-terminated.
>>
>>
>> This is a fixed-length string field that is not supposed to be
>> NUL-terminated.  The use of strncpy() here is deliberate in order to be able
>> to completely fill the field.
> 
> I thing people are going to assume NUL-terminated strings when string
> functions like strncpy() are involved.
> How about using memcpy() to tell both the compiler and the programmer
> that this is not a NUL-terminated string?

Yeah, I would then think memcpy would be the right function to use in that case.

Bruce




More information about the ipxe-devel mailing list