Re: [PATCH] gpu/drm: fix a -Wstringop-truncation warning

From: Qian Cai
Date: Thu Sep 05 2019 - 10:41:04 EST


Ping. Please take a look at this trivial patch.

On Fri, 2019-08-23 at 14:37 -0400, Qian Cai wrote:
> In file included from ./include/linux/bitmap.h:9,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./include/linux/cpumask.h:12,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./arch/x86/include/asm/cpumask.h:5,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./arch/x86/include/asm/msr.h:11,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./arch/x86/include/asm/processor.h:21,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./arch/x86/include/asm/cpufeature.h:5,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./arch/x86/include/asm/thread_info.h:53,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./include/linux/thread_info.h:38,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./arch/x86/include/asm/preempt.h:7,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./include/linux/preempt.h:78,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./include/linux/rcupdate.h:27,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./include/linux/rculist.h:11,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./include/linux/pid.h:5,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./include/linux/sched.h:14,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom ./include/linux/uaccess.h:5,
> ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂfrom drivers/gpu/drm/drm_property.c:24:
> In function 'strncpy',
> ÂÂÂÂinlined from 'drm_property_create' at
> drivers/gpu/drm/drm_property.c:130:2:
> ./include/linux/string.h:305:9: warning: '__builtin_strncpy' specified
> bound 32 equals destination size [-Wstringop-truncation]
> Â return __builtin_strncpy(p, q, size);
> ÂÂÂÂÂÂÂÂÂ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Fix it by using strscpy() which will always return a valid string, and
> doesn't unnecessarily force the tail of the destination buffer to be
> zeroed.
>
> Signed-off-by: Qian Cai <cai@xxxxxx>
> ---
> Âdrivers/gpu/drm/drm_property.c | 3 +--
> Â1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_property.c b/drivers/gpu/drm/drm_property.c
> index 892ce636ef72..66ec2cc7a559 100644
> --- a/drivers/gpu/drm/drm_property.c
> +++ b/drivers/gpu/drm/drm_property.c
> @@ -127,8 +127,7 @@ struct drm_property *drm_property_create(struct drm_device
> *dev,
> Â property->num_values = num_values;
> Â INIT_LIST_HEAD(&property->enum_list);
> Â
> - strncpy(property->name, name, DRM_PROP_NAME_LEN);
> - property->name[DRM_PROP_NAME_LEN-1] = '\0';
> + strscpy(property->name, name, DRM_PROP_NAME_LEN);
> Â
> Â list_add_tail(&property->head, &dev->mode_config.property_list);
> Â