diff mbox

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

Message ID 20180720025038.9365-7-honli@redhat.com (mailing list archive)
State Not Applicable
Headers show

Commit Message

Honggang LI July 20, 2018, 2:50 a.m. UTC
From: Honggang Li <honli@redhat.com>

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>
---
 opensm/osm_prtn.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Hal Rosenstock July 25, 2018, 11:59 a.m. UTC | #1
On 7/19/2018 10:50 PM, Honggang LI wrote:
> From: Honggang Li <honli@redhat.com>
> 
> 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>
> ---
>  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@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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