Message ID | 202301111425483027624@zte.com.cn |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp3156751wrt; Tue, 10 Jan 2023 22:27:47 -0800 (PST) X-Google-Smtp-Source: AMrXdXushoUNGahCpMpHRuAntwCFwga0t3ulp0EIQukLMDct5xziigqdDA2u/cZn9q9AaOI4Z1KB X-Received: by 2002:a17:906:99c7:b0:7ae:b637:633f with SMTP id s7-20020a17090699c700b007aeb637633fmr81990061ejn.4.1673418467321; Tue, 10 Jan 2023 22:27:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673418467; cv=none; d=google.com; s=arc-20160816; b=IkVhWQT2K3YtbVtAKpOsP6Fkn+CsAklCwecYqzPogSf/rBrcfKuJnX/nHikkR/kYdp puIxcPbh/sn2YMzcg2UODHv3rRRghr1b/6SypSUnRBcfBST+sTHYclYze/lLAQKeQl5w Z/Lidlx4Hnz1XRLb37qI3CuxGuv2kKYbYq+aEJ4uBSQ1BvMPWxg4l17l99PpNJBaPJy1 MupvygOZ1YyVkuUQZhNi+2hD06zl/dNvAwnUofozGQKvdZ+W+9BkXn/EDbaMv8WnW1Zr z5ck6FjDVA40FsquBrZ0xwEsI8iQ6fFJ6wLO6sJZXRB0fXtoMhx/xu43VyrATzCzNdxU w8aQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:subject:cc:to:from:mime-version:message-id:date; bh=HSaQbFEpkF8rj56HQeQPMDZyCt/oZGTE+zAvNEjfn7g=; b=MVv3+1nB/n0xbg1aFNiJGpStXznqdM8Joo3JabZt11fSnmc9XrVmNWBUdpR38vo3JU aSH+78gCkNTlmZGb17y4XV+ve9B2oBNni8YzLWwVo6kfCnNXV70sIxVEhnk+wF6hOztO nfDll9yIePP6N5+OnxnB4wNdaLqM5uq8bwn4l5azfG9hzJ4mpErqn5keyZshYWkfvYf+ LGw6KUElb0j3N4XFQYprPqcKO2tI13438adQdpizWDUtTCSlnHYf0nzdhTdfEowYlgws A/W8YHvVduJtQiPcQdnnU+KKfafBz+XxWYmlUApBktV0GwQSaomN3KIKJQh8J2L4ea8h IOSQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id wg4-20020a17090705c400b007adf3ed5d20si14310593ejb.376.2023.01.10.22.27.23; Tue, 10 Jan 2023 22:27:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=zte.com.cn Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234554AbjAKG0V (ORCPT <rfc822;syz17693488234@gmail.com> + 99 others); Wed, 11 Jan 2023 01:26:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231640AbjAKG0C (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 11 Jan 2023 01:26:02 -0500 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04241EB0; Tue, 10 Jan 2023 22:25:58 -0800 (PST) Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4NsHkD0k8sz8RV6K; Wed, 11 Jan 2023 14:25:56 +0800 (CST) Received: from szxlzmapp02.zte.com.cn ([10.5.231.79]) by mse-fl1.zte.com.cn with SMTP id 30B6Pkjg075836; Wed, 11 Jan 2023 14:25:46 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Wed, 11 Jan 2023 14:25:48 +0800 (CST) Date: Wed, 11 Jan 2023 14:25:48 +0800 (CST) X-Zmail-TransId: 2b0363be566c67d92096 X-Mailer: Zmail v1.0 Message-ID: <202301111425483027624@zte.com.cn> Mime-Version: 1.0 From: <yang.yang29@zte.com.cn> To: <santosh.shilimkar@oracle.com>, <kuba@kernel.org> Cc: <davem@davemloft.net>, <edumazet@google.com>, <pabeni@redhat.com>, <netdev@vger.kernel.org>, <linux-rdma@vger.kernel.org>, <rds-devel@oss.oracle.com>, <linux-kernel@vger.kernel.org>, <xu.panda@zte.com.cn>, <yang.yang29@zte.com.cn> Subject: =?utf-8?q?=5BPATCH_net-next_v2=5D_net/rds=3A_use_strscpy=28=29_to_i?= =?utf-8?q?nstead_of_strncpy=28=29?= Content-Type: text/plain; charset="UTF-8" X-MAIL: mse-fl1.zte.com.cn 30B6Pkjg075836 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.137.novalocal with ID 63BE5674.000 by FangMail milter! X-FangMail-Envelope: 1673418356/4NsHkD0k8sz8RV6K/63BE5674.000/10.5.228.132/[10.5.228.132]/mse-fl1.zte.com.cn/<yang.yang29@zte.com.cn> X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63BE5674.000/4NsHkD0k8sz8RV6K X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754706442661145416?= X-GMAIL-MSGID: =?utf-8?q?1754706442661145416?= |
Series |
[net-next,v2] net/rds: use strscpy() to instead of strncpy()
|
|
Commit Message
Yang Yang
Jan. 11, 2023, 6:25 a.m. UTC
From: Xu Panda <xu.panda@zte.com.cn> The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL-terminated strings. Signed-off-by: Xu Panda <xu.panda@zte.com.cn> Signed-off-by: Yang Yang <yang.yang29@zte.com.cn> --- change for v2 - Use the returns of strscpy to make the copy and the preceding BUG_ON() together.Thanks to Jakub Kicinski. --- net/rds/stats.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
Comments
On Wed, 11 Jan 2023 14:25:48 +0800 (CST) yang.yang29@zte.com.cn wrote: > From: Xu Panda <xu.panda@zte.com.cn> > > The implementation of strscpy() is more robust and safer. > That's now the recommended way to copy NUL-terminated strings. What are the differences in behavior between strncpy() and strscpy()? > diff --git a/net/rds/stats.c b/net/rds/stats.c > index 9e87da43c004..7018c67418f5 100644 > --- a/net/rds/stats.c > +++ b/net/rds/stats.c > @@ -88,9 +88,7 @@ void rds_stats_info_copy(struct rds_info_iterator *iter, > size_t i; > > for (i = 0; i < nr; i++) { > - BUG_ON(strlen(names[i]) >= sizeof(ctr.name)); > - strncpy(ctr.name, names[i], sizeof(ctr.name) - 1); > - ctr.name[sizeof(ctr.name) - 1] = '\0'; > + BUG_ON(strscpy(ctr.name, names[i], sizeof(ctr.name)) < 0); > ctr.value = values[i]; > > rds_info_copy(iter, &ctr, sizeof(ctr));
> What are the differences in behavior between strncpy() and strscpy()?
Strscpy() makes the dest string NUL-terminated, and returns more
useful value. While strncpy() can initialize the dest string.
Here we use strscpy() to make dest string NUL-terminated, and use
return value to check src string size and dest string size. This make
the code simpler.
On Fri, 13 Jan 2023 15:13:12 +0800 (CST) yang.yang29@zte.com.cn wrote: > > What are the differences in behavior between strncpy() and strscpy()? > > Strscpy() makes the dest string NUL-terminated, and returns more > useful value. While strncpy() can initialize the dest string. > > Here we use strscpy() to make dest string NUL-terminated, and use > return value to check src string size and dest string size. This make > the code simpler. I'm not sure whether in this particular case the output needs to be padded or not. And I'm not sure you understand what the implications are. The code is fine as is, and I don't trust that you know what you're doing. So please don't send any more strncpy() -> strscpy() conversions for networking. If you want to do something useful please start with adding a check to checkpatch to warn people against using strncpy() and suggest using strscpy() instead.
diff --git a/net/rds/stats.c b/net/rds/stats.c index 9e87da43c004..7018c67418f5 100644 --- a/net/rds/stats.c +++ b/net/rds/stats.c @@ -88,9 +88,7 @@ void rds_stats_info_copy(struct rds_info_iterator *iter, size_t i; for (i = 0; i < nr; i++) { - BUG_ON(strlen(names[i]) >= sizeof(ctr.name)); - strncpy(ctr.name, names[i], sizeof(ctr.name) - 1); - ctr.name[sizeof(ctr.name) - 1] = '\0'; + BUG_ON(strscpy(ctr.name, names[i], sizeof(ctr.name)) < 0); ctr.value = values[i]; rds_info_copy(iter, &ctr, sizeof(ctr));