From patchwork Wed Jul 25 12:04:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Hal Rosenstock X-Patchwork-Id: 10543907 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8CC83157A for ; Wed, 25 Jul 2018 12:04:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7C82929D37 for ; Wed, 25 Jul 2018 12:04:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 701EA29D3D; Wed, 25 Jul 2018 12:04:11 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 13BBC29D37 for ; Wed, 25 Jul 2018 12:04:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728910AbeGYNPc (ORCPT ); Wed, 25 Jul 2018 09:15:32 -0400 Received: from mail-qt0-f193.google.com ([209.85.216.193]:39971 "EHLO mail-qt0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728762AbeGYNPc (ORCPT ); Wed, 25 Jul 2018 09:15:32 -0400 Received: by mail-qt0-f193.google.com with SMTP id h4-v6so7293772qtj.7 for ; Wed, 25 Jul 2018 05:04:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dev-mellanox-co-il.20150623.gappssmtp.com; s=20150623; h=to:cc:from:subject:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=EDS2SEedXSUVIw1EENGnB8Rmc1PqMxNJZ4TY05/kVxw=; b=GVJnFW5hPIHO7+OATl7+JqxbuetAMG2oXFVxDYyGIMGxBFTOtvW1wwra8hCLWs25GN eIfe509xeZQVHh1v7QarR5IE/K6KpEHgxe/BVPtSYk9HY/ggpXUGk5lxXn+rMOzgtF1h QqFjLIRgTGDVXmXabOPZTnw2CGf0sE80a4ki2b7K1s33H+agL1H97k3daaxjW+Sp44+M hYDWxJ8tkJQQWpesBQO6s/d21d/3iCUm+vB5mJ3f/MMLXmUuHjxzmZBd8SAn0A52cGL5 d+bAKItSorbeEzny8SpihEDTj1CboSsmY3E8wmwEH+T/4bHxrDqQUc0GSLoPXaVYMyic 2sZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:from:subject:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=EDS2SEedXSUVIw1EENGnB8Rmc1PqMxNJZ4TY05/kVxw=; b=PIFV6zfnG/7zhV3yMJX/ZB7izqUtYlufuWaK+ef3XWAhsHsDyzQ/GgxIPGYHub35nF H+ibkhqxkcl29goXBpwulh0KsYaZnMmDWK5+HopvAoI8aafy0K5JfryECnDypEmmBO+f /d7UMxK3JVHv5rIM04DSAh2Yads9XLTAe8gnOJE57mpJaSWsTgWsKwZ0W+QR7xa5R1uE CmhCwY0gH0Oj8ucoH64hU56m2B+Ga3ly7i7xissrpQ3gP0iwzpWzPoFodg5UF7Gh9WHN Xn0TAbGaA9MgUvncMzy+G0LmEqo3sOrpK2Cg3TgmvUADkygAFaoGlxkZFyeuarz686gJ B6tw== X-Gm-Message-State: AOUpUlHFaog94U4CpceVxmhOhvPmqvKENP5dIse5AdxIZ3HThCA2W/gF 3bNzl5TQAbpGYQTwaiY3qHAHwyPJKNQ= X-Google-Smtp-Source: AAOMgpfMda//iX5XSTRBN0f+t+hW7eH5MdSjQzi0cmS3BrzC8Ksftx9MpIBIVCcmnL2lkgiBQgXWRw== X-Received: by 2002:ac8:53d2:: with SMTP id c18-v6mr20206519qtq.146.1532520248849; Wed, 25 Jul 2018 05:04:08 -0700 (PDT) Received: from [192.168.1.183] (c-73-142-227-196.hsd1.ma.comcast.net. [73.142.227.196]) by smtp.googlemail.com with ESMTPSA id p13-v6sm10023707qki.12.2018.07.25.05.04.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 Jul 2018 05:04:08 -0700 (PDT) To: Honggang LI Cc: "linux-rdma@vger.kernel.org" From: Hal Rosenstock Subject: [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> Date: Wed, 25 Jul 2018 08:04:07 -0400 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 Content-Language: en-US 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 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 Signed-off-by: Hal Rosenstock Acked-by: Honggang Li --- 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;