On 2/11/2020 6:04 AM, Richard Cochran wrote:
> When compiling with gcc8 and -O2, the clever compiler complains:
> 
>    interface.c: In function ‘interface_ensure_tslabel’:
>    interface.c:38:3: error: ‘strncpy’ output may be truncated copying 108 
> bytes from a string of length 108 [-Werror=stringop-truncation]
> 
> Even though this is a false positive, this patch silences the warning
> by using memcpy instead of strncpy.
> 

You could also use snprintf("%s", ..., sizeof(iface->name);

memcpy also copies all of the data even if it doesn't strictly need to.

Thanks,
Jake

> Signed-off-by: Richard Cochran <richardcoch...@gmail.com>
> ---
>  interface.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/interface.c b/interface.c
> index 7cd5b41..2cf3b1e 100644
> --- a/interface.c
> +++ b/interface.c
> @@ -35,7 +35,7 @@ void interface_destroy(struct interface *iface)
>  void interface_ensure_tslabel(struct interface *iface)
>  {
>       if (!iface->ts_label[0]) {
> -             strncpy(iface->ts_label, iface->name, MAX_IFNAME_SIZE);
> +             memcpy(iface->ts_label, iface->name, MAX_IFNAME_SIZE);
>       }
>  }
>  
> 


_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to