From patchwork Wed Jan 11 06:25:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yang X-Patchwork-Id: 13096123 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA790C5479D for ; Wed, 11 Jan 2023 06:26:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229956AbjAKG0N (ORCPT ); Wed, 11 Jan 2023 01:26:13 -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 ); 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: To: , Cc: , , , , , , , , 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?= 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/ X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 63BE5674.000/4NsHkD0k8sz8RV6K Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org From: Xu Panda 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 Signed-off-by: Yang Yang --- 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(-) 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));