From patchwork Fri Jul 20 02:50:38 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Honggang LI X-Patchwork-Id: 10535903 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DE4AC6029B for ; Fri, 20 Jul 2018 02:51:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CF8AB298A6 for ; Fri, 20 Jul 2018 02:51:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C3CDF299DA; Fri, 20 Jul 2018 02:51:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 46C21298A6 for ; Fri, 20 Jul 2018 02:51:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731103AbeGTDha (ORCPT ); Thu, 19 Jul 2018 23:37:30 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:41676 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730514AbeGTDha (ORCPT ); Thu, 19 Jul 2018 23:37:30 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7BD2872630; Fri, 20 Jul 2018 02:51:28 +0000 (UTC) Received: from dhcp-129-124.nay.redhat.com (unknown [10.66.129.124]) by smtp.corp.redhat.com (Postfix) with ESMTP id D4E74111E406; Fri, 20 Jul 2018 02:51:26 +0000 (UTC) From: Honggang LI To: hal@mellanox.com Cc: linux-rdma@vger.kernel.org, Honggang Li Subject: [PATCH 6/6] osm_prtn.c: To avoid potential no NUL-terminated strncpy Date: Fri, 20 Jul 2018 10:50:38 +0800 Message-Id: <20180720025038.9365-7-honli@redhat.com> In-Reply-To: <20180720025038.9365-6-honli@redhat.com> References: <20180720025038.9365-1-honli@redhat.com> <20180720025038.9365-2-honli@redhat.com> <20180720025038.9365-3-honli@redhat.com> <20180720025038.9365-4-honli@redhat.com> <20180720025038.9365-5-honli@redhat.com> <20180720025038.9365-6-honli@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 20 Jul 2018 02:51:28 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 20 Jul 2018 02:51:28 +0000 (UTC) for IP:'10.11.54.3' DOMAIN:'int-mx03.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'honli@redhat.com' RCPT:'' Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Honggang Li 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 --- 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));