Re: [PATCH 6/6] osm_prtn.c: To avoid potential no NUL-terminated strncpy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 7/19/2018 10:50 PM, Honggang LI wrote:
> From: Honggang Li <honli@xxxxxxxxxx>
> 
> make[2]: Entering directory '/home/honli/upstream-repos/opensm/opensm'
> depbase=`echo osm_prtn.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
> gcc -DHAVE_CONFIG_H -I. -I../include -I../include/opensm  -I./../include -I./../../libibumad/include -I/usr/local/include   -Werror -Wall -Wwrite-strings -g -D_XOPEN_SOURCE=600 -D_DEFAULT_SOURCE=1 -g -O2 -MT osm_prtn.o -MD -MP -MF $depbase.Tpo -c -o osm_prtn.o osm_prtn.c &&\
> mv -f $depbase.Tpo $depbase.Po
> osm_prtn.c: In function ‘osm_prtn_new’:
> osm_prtn.c:84:3: error: ‘strncpy’ specified bound 32 equals destination size [-Werror=stringop-truncation]
>    strncpy(p->name, name, sizeof(p->name));
>    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> cc1: all warnings being treated as errors
> 
> Signed-off-by: Honggang Li <honli@xxxxxxxxxx>
> ---
>  opensm/osm_prtn.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/opensm/osm_prtn.c b/opensm/osm_prtn.c
> index bc1814d6b6ed..3492a04494ee 100644
> --- a/opensm/osm_prtn.c
> +++ b/opensm/osm_prtn.c
> @@ -80,8 +80,12 @@ osm_prtn_t *osm_prtn_new(IN const char *name, IN uint16_t pkey)
>  	cl_map_construct(&p->part_guid_tbl);
>  	cl_map_init(&p->part_guid_tbl, 32);
>  
> -	if (name && *name)
> -		strncpy(p->name, name, sizeof(p->name));
> +	if (name && *name) {
> +		int cnt = strlen(name);
> +		if (cnt > sizeof(p->name)-1)
> +			cnt = sizeof(p->name)-1;
> +		strncpy(p->name, name, cnt);
> +	}
>  	else
>  		snprintf(p->name, sizeof(p->name), "%04x", cl_ntoh16(pkey));
>  
> 

A slightly different approach to solve this in patch to be posted shortly.

-- Hal
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Photo]     [Yosemite News]     [Yosemite Photos]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux