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

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

 



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>
Signed-off-by: Hal Rosenstock <hal@xxxxxxxxxxxx>
---
 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;
-- 
2.8.4

--
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