Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp1848595rwb; Sun, 4 Sep 2022 03:37:58 -0700 (PDT) X-Google-Smtp-Source: AA6agR6/iQudajBOYTHMnpB7Id21vUFITp3BNNLE6nqLhlT5wa01hCegR9/nrsEsHrVsPeQbYqn8 X-Received: by 2002:a17:902:ec06:b0:176:6917:818b with SMTP id l6-20020a170902ec0600b001766917818bmr10675635pld.74.1662287878596; Sun, 04 Sep 2022 03:37:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662287878; cv=none; d=google.com; s=arc-20160816; b=p/mBLPYXbPwB80BXjBsLgne0wK1AlzLYb6lQ6DLuJpy3CbcXkiwYdwruQ063z+wgVS TOfTUpnl0l/xR34JwlhTZP4s3P2/a6MIyM03l47YwPbdArnRp0YjHMSfa5mC5nprNUmr aKgTrlfec+MvwyNMcnh19Q2VVgRwzXVgQpM2skRJw0+hDtuzcUWeJAoTM8osvhURyXTn bRYwEJPTkGeY0sYNJBL4WOOk8ssdrsMHrHx6pjwzWs/pwE8/EejFzlo8jotu8IF0Wz2B mnP5unjN4pOtoSQtaXq6w70w3uP4vHonhTTKBtZqUHYElRg88UBu6ID/sg0Z3CwgmjmV gK8A== 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=Hc6i9HsVnqTI+5s+WVEg+1gkvfBoroMwIXKrvibOmgY=; b=mmN989pgnQTnHdYXiDcAz0CFh6iYV+3Ev12/OhIt+9DYiOYEIYr1xnanQkEHiGd+F6 vgRTV9Z0DNlaVS4B/1gVqw74zWvP5LX9Vzcwn31TsRdvBUYpiiQyghNv36PAjRk/Tn/4 m/2cG2b2U3u/PxgMN+8MCKDTE5EU8Y0foZ2z7d2Llhe83sIX7XtStf1qbGDsbnp53tU2 yO3MZknRknTRNZB/nWHmkNDO+Us/Mk1NWJl94ix6JyVrB0m6zUfAXCtZbLjXpL/EwpZ2 aL0MEM15p29qYzTowq+YlKKQcOgecZjrm4EW+mY9AOHnN/4wVJ+BgG++HyFGRgzxBLHV u9Sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OzOU7cyI; 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 q33-20020a17090a4fa400b001f270af836esi7259535pjh.139.2022.09.04.03.37.47; Sun, 04 Sep 2022 03:37:58 -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=OzOU7cyI; 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 S233849AbiIDKTK (ORCPT + 99 others); Sun, 4 Sep 2022 06:19:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229569AbiIDKTG (ORCPT ); Sun, 4 Sep 2022 06:19:06 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2595F4598D for ; Sun, 4 Sep 2022 03:19:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662286745; x=1693822745; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qwD2MnFCCPaeWqxY3gsH7fYiNvcGic+bi1IDybKnebc=; b=OzOU7cyI61tIAZ3eyyyvkqaILeZG/pK1zVRvDRFLmF/+WdFPrquSdGyn t9N6Z0PvbuFdTNGpp+8LotcGnzrvffbgvGQqzjUMWqB0CJMgEuc/evo+b EqKzN0DpDJCiL2MdDus3AxP6P6gaRvEIFXRgdR0K3W13jcxm0wIhnuS2c MG/yxx2TkJZU3OOt7ZjvI077W1PyVj/KsOA8jPDMu5gY/c+NFrCYPAOuq Z6ypDOcl0ZW1bYHUtDrt0ll466DebOFWT/U5/NXqkZFtGa6uBVfpoGZP8 lDTQB0jnM6i8u7hfNgl78YBPR8x2dDYJNj4e6JxjyknNRDX2THgkny4gN A==; X-IronPort-AV: E=McAfee;i="6500,9779,10459"; a="382529100" X-IronPort-AV: E=Sophos;i="5.93,289,1654585200"; d="scan'208";a="382529100" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Sep 2022 03:19:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,289,1654585200"; d="scan'208";a="646589511" Received: from crojewsk-ctrl.igk.intel.com ([10.102.9.28]) by orsmga001.jf.intel.com with ESMTP; 04 Sep 2022 03:19:00 -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 v6 1/2] lib/string_helpers: Introduce parse_int_array_user() Date: Sun, 4 Sep 2022 12:28:39 +0200 Message-Id: <20220904102840.862395-2-cezary.rojewski@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220904102840.862395-1-cezary.rojewski@intel.com> References: <20220904102840.862395-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_NONE,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 | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 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..230020a2e076 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -131,6 +131,50 @@ 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 + * @count: The maximum number of bytes to read + * @array: Returned pointer to sequence of integers + * + * On success @array 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 @array 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