From patchwork Sun Sep 4 10:28:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cezary Rojewski X-Patchwork-Id: 12965177 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 alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 11A90ECAAD4 for ; Sun, 4 Sep 2022 10:20:29 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 33E9F1669; Sun, 4 Sep 2022 12:19:37 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 33E9F1669 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1662286827; bh=qwD2MnFCCPaeWqxY3gsH7fYiNvcGic+bi1IDybKnebc=; h=From:To:Subject:Date:In-Reply-To:References:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=sPyKE2r6l8z8wDVZ906P/V4sfqX/XD172phVeqMs297ndk43CFhBKPjoECmIJzta1 NBC2SXV5oxRdpciVfAdZztZYlNiKbnYQi7pt5QzHyAm4ll5/Uhu5XptmdlSFswqFfp Xoq4Q2HQ5d2KR6F0oIJ98OmznF+nrC/HF0pexYXk= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 59689F802BE; Sun, 4 Sep 2022 12:19:15 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id CF858F80430; Sun, 4 Sep 2022 12:19:13 +0200 (CEST) Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 31EFAF80124 for ; Sun, 4 Sep 2022 12:19:06 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 31EFAF80124 Authentication-Results: alsa1.perex.cz; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="crQZ9aRg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662286748; x=1693822748; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qwD2MnFCCPaeWqxY3gsH7fYiNvcGic+bi1IDybKnebc=; b=crQZ9aRgUZlI5SSlnP1foJ1l+6lfL+KJ40+bRX3r+TbWnzEOuYHpOH6U pGgCL3NhHj2Ha/tieIkpAo//B3V5VTtP3PtJniNCUVl7HWv4R+4IoZb/Y bkINLr4A757p417nqG8rGiuSWRUGqZ/uEbKHc3/ftq7BuOdXR3wpCIaHF FpusKlJqeFkVYdzyZdwsgSbnziIIGBe6NKemEPx/59XzidjLwF6+oQ4Wq VWVVmT8x/SrK6bXyT3d/NCf+qX9oYWkC5IF2nlpnHh0ZeZpIp3My7wq45 lR4qjZAbNTueZLaXZYJcS5cB5Yh4XJIQU3yb9qeIdqBw7HGvd0EgGv4vG Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10459"; a="360185151" X-IronPort-AV: E=Sophos;i="5.93,289,1654585200"; d="scan'208";a="360185151" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.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 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 Cc: andy@kernel.org, Cezary Rojewski , intel-poland@eclists.intel.com, kai.vehmanen@linux.intel.com, yung-chuan.liao@linux.intel.com, tiwai@suse.com, pierre-louis.bossart@linux.intel.com, willy@infradead.org, lgirdwood@gmail.com, hdegoede@redhat.com, andy.shevchenko@gmail.com, ranjani.sridharan@linux.intel.com, amadeuszx.slawinski@linux.intel.com, peter.ujfalusi@linux.intel.com, linux-kernel@vger.kernel.org X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" 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;