From nobody Sun Sep 14 14:51:20 2025 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 CF318C46467 for ; Fri, 20 Jan 2023 02:59:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229654AbjATC7q (ORCPT ); Thu, 19 Jan 2023 21:59:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229613AbjATC7n (ORCPT ); Thu, 19 Jan 2023 21:59:43 -0500 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F1C29EE3D; Thu, 19 Jan 2023 18:59:40 -0800 (PST) Received: from dggpemm100011.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Nykgz2T41zZfn7; Fri, 20 Jan 2023 10:57:51 +0800 (CST) Received: from huawei.com (10.175.127.227) by dggpemm100011.china.huawei.com (7.185.36.112) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.34; Fri, 20 Jan 2023 10:59:37 +0800 From: Li Lingfeng To: CC: , , , , , , , , , , , , Subject: [PATCH-next v4] lib: parser: optimize match_NUMBER apis to use local array Date: Fri, 20 Jan 2023 11:23:52 +0800 Message-ID: <20230120032352.242767-1-lilingfeng3@huawei.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.127.227] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm100011.china.huawei.com (7.185.36.112) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Memory will be allocated to store substring_t in match_strdup(), which means the caller of match_strdup() may need to be scheduled out to wait for reclaiming memory. Using local array to store substring_t to remove the restriction. Link: https://lore.kernel.org/all/20221104023938.2346986-5-yukuai1@huaweicl= oud.com/ Signed-off-by: Li Lingfeng --- v1->v2: change the name of buffer's length use match_strlcpy() to copy string and keep string length check v2->v3: judge whether the length to be copied exceeds the limit by the=20 return value of match_strlcpy() v3->v4: fix typos in title format the commit message complete the modification which should be done in v3 lib/parser.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/parser.c b/lib/parser.c index bcb23484100e..2b5e2b480253 100644 --- a/lib/parser.c +++ b/lib/parser.c @@ -11,6 +11,15 @@ #include #include =20 +/* + * max size needed by different bases to express U64 + * HEX: "0xFFFFFFFFFFFFFFFF" --> 18 + * DEC: "18446744073709551615" --> 20 + * OCT: "01777777777777777777777" --> 23 + * pick the max one to define NUMBER_BUF_LEN + */ +#define NUMBER_BUF_LEN 24 + /** * match_one - Determines if a string matches a simple pattern * @s: the string to examine for presence of the pattern @@ -129,14 +138,12 @@ EXPORT_SYMBOL(match_token); static int match_number(substring_t *s, int *result, int base) { char *endp; - char *buf; + char buf[NUMBER_BUF_LEN]; int ret; long val; =20 - buf =3D match_strdup(s); - if (!buf) - return -ENOMEM; - + if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >=3D NUMBER_BUF_LEN) + return -ERANGE; ret =3D 0; val =3D simple_strtol(buf, &endp, base); if (endp =3D=3D buf) @@ -145,7 +152,6 @@ static int match_number(substring_t *s, int *result, in= t base) ret =3D -ERANGE; else *result =3D (int) val; - kfree(buf); return ret; } =20 @@ -163,18 +169,15 @@ static int match_number(substring_t *s, int *result, = int base) */ static int match_u64int(substring_t *s, u64 *result, int base) { - char *buf; + char buf[NUMBER_BUF_LEN]; int ret; u64 val; =20 - buf =3D match_strdup(s); - if (!buf) - return -ENOMEM; - + if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >=3D NUMBER_BUF_LEN) + return -ERANGE; ret =3D kstrtoull(buf, base, &val); if (!ret) *result =3D val; - kfree(buf); return ret; } =20 @@ -206,14 +209,12 @@ EXPORT_SYMBOL(match_int); */ int match_uint(substring_t *s, unsigned int *result) { - int err =3D -ENOMEM; - char *buf =3D match_strdup(s); + char buf[NUMBER_BUF_LEN]; =20 - if (buf) { - err =3D kstrtouint(buf, 10, result); - kfree(buf); - } - return err; + if (match_strlcpy(buf, s, NUMBER_BUF_LEN) >=3D NUMBER_BUF_LEN) + return -ERANGE; + + return kstrtouint(buf, 10, result); } EXPORT_SYMBOL(match_uint); =20 --=20 2.31.1