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

Michael Brown mcb30 at ipxe.org
Mon Apr 23 17:53:11 UTC 2018


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.

Is there a (clean) way to indicate to gcc to ignore this false positive 
warning?

Thanks,

Michael



More information about the ipxe-devel mailing list