Message ID | 202301091945513559977@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 p1csp2108475wrt; Mon, 9 Jan 2023 03:49:54 -0800 (PST) X-Google-Smtp-Source: AMrXdXsL7j+ct+ixc5p4IhrFqRzr5tziemgfFpmEcQ9Ivsm+PwxfAXOjJTwleRsqDmVKPIx80SGb X-Received: by 2002:a17:902:6ac3:b0:192:f5f3:4edf with SMTP id i3-20020a1709026ac300b00192f5f34edfmr16411693plt.38.1673264993938; Mon, 09 Jan 2023 03:49:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673264993; cv=none; d=google.com; s=arc-20160816; b=VAWb48OtSEbX99FEzsfHJ9bCcbeFwjJ3Y/vwnqUYnVneeNgqZL62c1jR7KXD95QX5k kcm3ToFKdQmlb/tBnpVjp7U7SgDBoifJ1FrXQ2u9k32joaMX4w+BDseQ0xhDlt7Yo4Yg sW5Upa9HV1I+b/VSeDQ6OXCw5lLzk7KSSk1Pn3aklRdrGjCRJa6CUD7cqWO2EUNh35Im IACdpufQvIT/53/vOkukuZcBdyKqbKrrgIjXPzZQOk2pquyMSRNGU+RnyYqVNYEObPdx 9fALuPuWmX8pi3hI8jbd1nL9RyeZiaaJ+GfDa4CgH8eZ1phKWf63utfdwgX//ldpts7P Tpgg== 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=r9QtKw4MkZycSjNtMEoTxPG34xiIhQbRT5CqU+QDo9M=; b=qgO4MvRNDUUDqWLfrEQs9oTk/CnPgMP4uhOvJPcljYbrPFnt4/0nELVG7buQIH5hQO fEGDVZP2vfFZynbJJWtiFRvrTRayv3bFNvR5VQZwR6GMrbNbc8FWa9YCW9TL8Ymt3S5H IY47DlI42o30OFrGvWlWkPaiT5mHCVw1iKr8mcZfr1+ZLaWUJyfacujDg7shB0kju8fC 9Xoq77EAIC7EjYFPaqff24KJ0Hp9ArnYfekSG6QzSQKbyFBBnl5IpFM4BRSQk68kduEM WXbcAr/NUkHAJfEtNnTfo/izf6f3Yoif31YCW2ddIvRT5ooFns+5caC8Y10z4ojvosg6 ZQOg== 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 jh19-20020a170903329300b00174ea015ee8si8216652plb.592.2023.01.09.03.49.41; Mon, 09 Jan 2023 03:49:53 -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 S233686AbjAILqI (ORCPT <rfc822;zhanglyra.2023@gmail.com> + 99 others); Mon, 9 Jan 2023 06:46:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234245AbjAILp6 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 9 Jan 2023 06:45:58 -0500 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [63.216.63.35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00824200B for <linux-kernel@vger.kernel.org>; Mon, 9 Jan 2023 03:45:56 -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 4NrBwM51jlz6FK2T; Mon, 9 Jan 2023 19:45:55 +0800 (CST) Received: from szxlzmapp02.zte.com.cn ([10.5.231.79]) by mse-fl1.zte.com.cn with SMTP id 309Bjmri092787; Mon, 9 Jan 2023 19:45:48 +0800 (+08) (envelope-from yang.yang29@zte.com.cn) Received: from mapi (szxlzmapp01[null]) by mapi (Zmail) with MAPI id mid14; Mon, 9 Jan 2023 19:45:51 +0800 (CST) Date: Mon, 9 Jan 2023 19:45:51 +0800 (CST) X-Zmail-TransId: 2b0363bbfe6fffffffff89cdc856 X-Mailer: Zmail v1.0 Message-ID: <202301091945513559977@zte.com.cn> Mime-Version: 1.0 From: <yang.yang29@zte.com.cn> To: <perex@perex.cz> Cc: <tiwai@suse.com>, <alsa-devel@alsa-project.org>, <linux-kernel@vger.kernel.org>, <xu.panda@zte.com.cn>, <yang.yang29@zte.com.cn> Subject: =?utf-8?q?=5BPATCH_linux-next=5D_ALSA=3A_control-led=3A_use_strscpy?= =?utf-8?q?=28=29_to_instead_of_strncpy=28=29?= Content-Type: text/plain; charset="UTF-8" X-MAIL: mse-fl1.zte.com.cn 309Bjmri092787 X-Fangmail-Gw-Spam-Type: 0 X-FangMail-Miltered: at cgslv5.04-192.168.250.138.novalocal with ID 63BBFE73.000 by FangMail milter! X-FangMail-Envelope: 1673264755/4NrBwM51jlz6FK2T/63BBFE73.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: 63BBFE73.000/4NrBwM51jlz6FK2T X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,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?1754545514208728428?= X-GMAIL-MSGID: =?utf-8?q?1754545514208728428?= |
Series |
[linux-next] ALSA: control-led: use strscpy() to instead of strncpy()
|
|
Commit Message
Yang Yang
Jan. 9, 2023, 11:45 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> --- sound/core/control_led.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Comments
On Mon, 09 Jan 2023 12:45:51 +0100, <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. > > Signed-off-by: Xu Panda <xu.panda@zte.com.cn> > Signed-off-by: Yang Yang <yang.yang29@zte.com.cn> > --- > sound/core/control_led.c | 3 +-- > 1 file changed, 1 insertion(+), 2 deletions(-) > > diff --git a/sound/core/control_led.c b/sound/core/control_led.c > index f975cc85772b..c88653c205eb 100644 > --- a/sound/core/control_led.c > +++ b/sound/core/control_led.c > @@ -534,8 +534,7 @@ static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, si > struct snd_ctl_elem_id id; > int err; > > - strncpy(buf2, buf, len); > - buf2[len] = '\0'; > + strncpy(buf2, buf, len + 1); Still using strncpy()...? thanks, Takashi
On 09. 01. 23 13:40, Takashi Iwai wrote: > On Mon, 09 Jan 2023 12:45:51 +0100, > <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. >> >> Signed-off-by: Xu Panda <xu.panda@zte.com.cn> >> Signed-off-by: Yang Yang <yang.yang29@zte.com.cn> >> --- >> sound/core/control_led.c | 3 +-- >> 1 file changed, 1 insertion(+), 2 deletions(-) >> >> diff --git a/sound/core/control_led.c b/sound/core/control_led.c >> index f975cc85772b..c88653c205eb 100644 >> --- a/sound/core/control_led.c >> +++ b/sound/core/control_led.c >> @@ -534,8 +534,7 @@ static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, si >> struct snd_ctl_elem_id id; >> int err; >> >> - strncpy(buf2, buf, len); >> - buf2[len] = '\0'; >> + strncpy(buf2, buf, len + 1); > > Still using strncpy()...? The original code should be: diff --git a/sound/core/control_led.c b/sound/core/control_led.c index f975cc85772b..b44abefcb593 100644 --- a/sound/core/control_led.c +++ b/sound/core/control_led.c @@ -530,7 +530,7 @@ static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, si bool attach) { char buf2[256], *s, *os; - size_t len = max(sizeof(s) - 1, count); + size_t len = min(sizeof(buf2) - 1, count); struct snd_ctl_elem_id id; int err; But it can be replaced with: diff --git a/sound/core/control_led.c b/sound/core/control_led.c index f975cc85772b..41d4e898d34c 100644 --- a/sound/core/control_led.c +++ b/sound/core/control_led.c @@ -530,12 +530,11 @@ static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, si bool attach) { char buf2[256], *s, *os; - size_t len = max(sizeof(s) - 1, count); struct snd_ctl_elem_id id; int err; - strncpy(buf2, buf, len); - buf2[len] = '\0'; + if (strscpy(buf2, buf, count) < 0) + return -E2BIG; memset(&id, 0, sizeof(id)); id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; s = buf2; I'll send a patch ASAP. Jaroslav -- Jaroslav Kysela <perex@perex.cz> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
diff --git a/sound/core/control_led.c b/sound/core/control_led.c index f975cc85772b..c88653c205eb 100644 --- a/sound/core/control_led.c +++ b/sound/core/control_led.c @@ -534,8 +534,7 @@ static ssize_t set_led_id(struct snd_ctl_led_card *led_card, const char *buf, si struct snd_ctl_elem_id id; int err; - strncpy(buf2, buf, len); - buf2[len] = '\0'; + strncpy(buf2, buf, len + 1); memset(&id, 0, sizeof(id)); id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; s = buf2;