Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1976897rwe; Fri, 2 Sep 2022 06:53:36 -0700 (PDT) X-Google-Smtp-Source: AA6agR6moRE5bHOYZ683pJRMhvKkuKWHdqGd1K30k9eB3KyLAcBoOF7gxIiL1I2GA3uyzNVVGcI3 X-Received: by 2002:a63:591b:0:b0:430:8421:f0e3 with SMTP id n27-20020a63591b000000b004308421f0e3mr6290058pgb.559.1662126816404; Fri, 02 Sep 2022 06:53:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662126816; cv=none; d=google.com; s=arc-20160816; b=o03iYXSqbwVvgxD7cwHWXWd49vblPrV7g5rJgNQy8d/j6Ow9N+BEP9mzgzFax9lXjD 9w/LHcCx9AySOTrNMQU5c/hfhPP5xxnfo4Hu5y1RpZN7BW4d7Ik1IbZnevj2vTdRGw9G 2GeTwsasew2wtLMkI1+diLTUCEEYr3yTYwVOsEkpKe1OPRTItzxErv0hDp4oRSJjSr+X QxY3SXgSbYX8l2rO3v8m9vaEYYWfnJDAVwC1WOC1honTVLiG8zXCz7U2Jy8HPB2JpIX5 PGgXe2qv1ztrajST6el7aDZkSb2IhJSpFdIc6jfnHpB+Reekjt9Jo5KPW4gis4n+mk5i BTAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=cFyKGpNBbnSXwVU7bmA3nLJ88o4a3ACjZJ6DDoQ1dUw=; b=uBSzCvNE5do7Um9SotZYlFJNGeqnIMQe5sLGdjYY81+2oyxUbUgGFQiBipzmMejRcI VrtWyUhQcEmUQVlP2D6yY3s1pK/40wtjkwQFB69z9z7PnSHFNSp+S/r6S9q1FGKEe/vU C+H3Kl2F7RG+pk3hoI5uO1laq1QoEPoH1IxBaxVW9aM4e+FLu85KyR+5ScoNjtRaS7tz UVaVWBmIMspzcVrkRtOzRzweV3TYsD89XS0DZ1PY2lWspvZm5ZXtaJdJLIePETWSpRq/ XwEs/0KoXH4dfDb5Me9cPQmHbYKucWf0FcWZANqi+OHIFSASQxNAAUsGzEdzhBWOmWXN plGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XHnE2Bly; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g4-20020a1709026b4400b0016f16417f6dsi1999276plt.16.2022.09.02.06.53.25; Fri, 02 Sep 2022 06:53:36 -0700 (PDT) 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; dkim=pass header.i=@intel.com header.s=Intel header.b=XHnE2Bly; 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=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236261AbiIBMh6 (ORCPT + 99 others); Fri, 2 Sep 2022 08:37:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237096AbiIBMhV (ORCPT ); Fri, 2 Sep 2022 08:37:21 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 188A662DF for ; Fri, 2 Sep 2022 05:19:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662121194; x=1693657194; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iito/a+ymZLPMsxae6Mq4cZS1ef+cun+eIfLQGc3m4I=; b=XHnE2BlyUKK7kyvmzMDwujYhOQOXV+XXBz7r7j1pLW1fBZFgDC1wDHhd TsZ3tyllsOq0skKf4sSQGAAVzkd30L2EvNUgLDCbHAB1luHKfKjkp6H56 WGJ+a5WeM1sYD+jU9QTSjkx/SrcXhUrHmKAglqpGNvQaDZmooOZYZ2KQw /0Ewb/8oM5Y9Pw/N+BSfkD0SFQocfXDL9MIp9JV1aeAiV2geHornaCg1O fl1P4zH0tPt69sWwqHvxlavBabSO2q+H+aKc/DcPPQE8bqqBRT1aFxmdO wjgDo5im+JDbfXzaGIqia/7Do6b74PXSuuIPpoNVQSK6/E1zkCIsACRNs A==; X-IronPort-AV: E=McAfee;i="6500,9779,10457"; a="275712444" X-IronPort-AV: E=Sophos;i="5.93,283,1654585200"; d="scan'208";a="275712444" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Sep 2022 05:19:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,283,1654585200"; d="scan'208";a="563946995" Received: from crojewsk-ctrl.igk.intel.com ([10.102.9.28]) by orsmga003.jf.intel.com with ESMTP; 02 Sep 2022 05:19:49 -0700 From: Cezary Rojewski To: alsa-devel@alsa-project.org, broonie@kernel.org Cc: tiwai@suse.com, perex@perex.cz, amadeuszx.slawinski@linux.intel.com, pierre-louis.bossart@linux.intel.com, hdegoede@redhat.com, lgirdwood@gmail.com, kai.vehmanen@linux.intel.com, peter.ujfalusi@linux.intel.com, ranjani.sridharan@linux.intel.com, yung-chuan.liao@linux.intel.com, willy@infradead.org, linux-kernel@vger.kernel.org, andy@kernel.org, intel-poland@eclists.intel.com, andy.shevchenko@gmail.com, Cezary Rojewski Subject: [PATCH v4 1/2] lib/string_helpers: Introduce parse_int_array_user() Date: Fri, 2 Sep 2022 14:29:27 +0200 Message-Id: <20220902122928.632602-2-cezary.rojewski@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220902122928.632602-1-cezary.rojewski@intel.com> References: <20220902122928.632602-1-cezary.rojewski@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_NONE,T_SCC_BODY_TEXT_LINE 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: X-Mailing-List: linux-kernel@vger.kernel.org Add new helper function to allow for splitting specified user string into a sequence of integers. Internally it makes use of get_options() so the returned sequence contains the integers extracted plus an additional element that begins the sequence and specifies the integers count. Suggested-by: Andy Shevchenko Reviewed-by: Andy Shevchenko Signed-off-by: Cezary Rojewski --- include/linux/string_helpers.h | 2 ++ lib/string_helpers.c | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h index 4d72258d42fd..dc2e726fd820 100644 --- a/include/linux/string_helpers.h +++ b/include/linux/string_helpers.h @@ -21,6 +21,8 @@ enum string_size_units { void string_get_size(u64 size, u64 blk_size, enum string_size_units units, char *buf, int len); +int parse_int_array_user(const char __user *from, size_t count, int **array); + #define UNESCAPE_SPACE BIT(0) #define UNESCAPE_OCTAL BIT(1) #define UNESCAPE_HEX BIT(2) diff --git a/lib/string_helpers.c b/lib/string_helpers.c index 5ed3beb066e6..d0c8f6ecf84c 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -131,6 +131,51 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units, } EXPORT_SYMBOL(string_get_size); +/** + * parse_int_array_user - Split string into a sequence of integers + * @from: The user space buffer to read from + * @ppos: The current position in the buffer + * @count: The maximum number of bytes to read + * @tkns: Returned pointer to sequence of integers + * + * On success @tkns is allocated and initialized with a sequence of + * integers extracted from the @from plus an additional element that + * begins the sequence and specifies the integers count. + * + * Caller takes responsibility for freeing @tkns when it is no longer + * needed. + */ +int parse_int_array_user(const char __user *from, size_t count, int **array) +{ + int *ints, nints; + char *buf; + int ret = 0; + + buf = memdup_user_nul(from, count); + if (IS_ERR(buf)) + return PTR_ERR(buf); + + get_options(buf, 0, &nints); + if (!nints) { + ret = -ENOENT; + goto free_buf; + } + + ints = kcalloc(nints + 1, sizeof(*ints), GFP_KERNEL); + if (!ints) { + ret = -ENOMEM; + goto free_buf; + } + + get_options(buf, nints + 1, ints); + *array = ints; + +free_buf: + kfree(buf); + return ret; +} +EXPORT_SYMBOL(parse_int_array_user); + static bool unescape_space(char **src, char **dst) { char *p = *dst, *q = *src; -- 2.25.1