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

Thomas Miletich thomas.miletich at gmail.com
Mon Apr 23 19:04:03 UTC 2018


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?

Just a quick thought.

Thomas


> Is there a (clean) way to indicate to gcc to ignore this false positive
> warning?
>
> Thanks,
>
> Michael
>
> _______________________________________________
> ipxe-devel mailing list
> ipxe-devel at lists.ipxe.org
> https://lists.ipxe.org/mailman/listinfo.cgi/ipxe-devel



More information about the ipxe-devel mailing list