diff mbox series

[PATCHv2,opensm] osm_prtn.c: Avoid potential no NUL-terminated strncpy in osm_prtn_new

Message ID 31d1055a-0436-9ff4-32a0-8462a4b8f06f@dev.mellanox.co.il (mailing list archive)
State Not Applicable
Headers show
Series [PATCHv2,opensm] osm_prtn.c: Avoid potential no NUL-terminated strncpy in osm_prtn_new | expand

Commit Message

Hal Rosenstock July 25, 2018, 12:04 p.m. UTC
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@redhat.com>
Signed-off-by: Hal Rosenstock <hal@mellanox.com>
---
 opensm/osm_prtn.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Honggang LI July 26, 2018, 12:08 a.m. UTC | #1
On Wed, Jul 25, 2018 at 08:04:07AM -0400, Hal Rosenstock wrote:
> 
> 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@redhat.com>
> Signed-off-by: Hal Rosenstock <hal@mellanox.com>
> ---
>  opensm/osm_prtn.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/opensm/osm_prtn.c b/opensm/osm_prtn.c
> index bc1814d..a203dff 100644
> --- a/opensm/osm_prtn.c
> +++ b/opensm/osm_prtn.c
> @@ -80,9 +80,10 @@ 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));
> -	else
> +	if (name && *name) {
> +		strncpy(p->name, name, sizeof(p->name) - 1);
> +		p->name[sizeof(p->name) - 1] = '\0';
> +	} else
>  		snprintf(p->name, sizeof(p->name), "%04x", cl_ntoh16(pkey));
>  
>  	return p;

Acked-by: Honggang Li <honli@redhat.com>

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

Patch

diff --git a/opensm/osm_prtn.c b/opensm/osm_prtn.c
index bc1814d..a203dff 100644
--- a/opensm/osm_prtn.c
+++ b/opensm/osm_prtn.c
@@ -80,9 +80,10 @@  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));
-	else
+	if (name && *name) {
+		strncpy(p->name, name, sizeof(p->name) - 1);
+		p->name[sizeof(p->name) - 1] = '\0';
+	} else
 		snprintf(p->name, sizeof(p->name), "%04x", cl_ntoh16(pkey));
 
 	return p;