Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1023924rwe; Thu, 1 Sep 2022 11:10:06 -0700 (PDT) X-Google-Smtp-Source: AA6agR4sdEDz/XrOh/a0ZA162gytiWIdNBJCN2xmKpEm4iboBMwgqk51ozZzjCybejtIG1aOl5XY X-Received: by 2002:a05:6402:27d3:b0:43e:5490:27ca with SMTP id c19-20020a05640227d300b0043e549027camr30609137ede.307.1662055805891; Thu, 01 Sep 2022 11:10:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662055805; cv=none; d=google.com; s=arc-20160816; b=M0fWNf0c35h64eZLwH6lvzBUonIyHitTF8Aao3ygyNJUqtJlNn7jX39Cck8iCLwmvL 3qySC0sM3mo7pSNFj3ZbM1I+7TpMMLnD535H+KHJM0Si+/m5Ng4dM0R1di9tA3d76154 SuHPD5sSMWxE9+5Toi3GU0CZVvvKqiaRMJoVNwd9/PMt9bYV041MXz+nMO92nxli2CPu LV90mBqIfRr2LlIRJtEK4z8nGTTSO/bOjAdVbKg7ZYW5yROTPT9eauCDFrOn4YqYwYcN 9eHGpiAaIyOvr07EapqZ2d6pgSYHdpiL9agJCbnL+udSU4IZ4QC9PJT5elE+PFjl8dzz yfDw== 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=YTC03jcLsOefMGVFU/ECVb9Me4rdeX+HPQxplWbQOjc=; b=01OqjxfHDeYrctqfPmCPfeH+5oN2bXg5+v88Vgkwt75TMNCRh0i5/oNONG75Q+el76 1hDdiLtV6PvDrD7YWCocYNd/0QNA+EnJ4z9nGirkvQqDTTSd26k6NgNGxSaxGdLeJM19 UBCXGp7/OikLQcTgiMFoNOMQvIl156T3R4+hxqWq7kW/SHSZeJKQc5D9xiuD5QzI1ZYc 7ha0krbRK7v8Tij4yfr4zlcvfd+FTI5lnsGct8REC/T0AHyJeJCp+WjumL13mWfwH5fo CayP4WpmMlfxE3wR4UlrRknp8XSFqHi7Rd5iNGgvrFPOsToMWH+qDizgsyoaWqeTvas5 Bd/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=b+qQpURo; 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 go19-20020a1709070d9300b00741688254f4si12128703ejc.163.2022.09.01.11.09.39; Thu, 01 Sep 2022 11:10:05 -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=b+qQpURo; 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 S234312AbiIARo1 (ORCPT + 99 others); Thu, 1 Sep 2022 13:44:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234301AbiIARnV (ORCPT ); Thu, 1 Sep 2022 13:43:21 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E035D96FC5 for ; Thu, 1 Sep 2022 10:40:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662054059; x=1693590059; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QpLq53kDMbsQj6L9fHeAwbwgO0XWugT9uvq/PRVvdGI=; b=b+qQpURoZ3u8TLQ5Ss4yIyQ+gZaTIBb9qbBZ9f6G1OzxkU+bbBe380JL mU7qtbE5v86u9yEcuSlhrNP7bsp08AOaL1EgmAFbVuZKyF+2avgXwccRI kjljGhvNZE9CuP5tr+i3aJHTmrLDBqiGuJC710ZFk76gIucEtYw4/YBAL hPUUNaQJYYNDD60e4lldycEgVHCWN+3NALiO8oqjdTyBFYy4q40zGsiQN p0yC1tfpApuzIdyUr8d6YqekJbTVX7UrNjyAAah5xItMPn1VxhtagPlDK 4EaBchLq12yx+CWi8t+Hi1NZx4+ht1Le5zsjdinZwbfWrvN0LPBMhe3Rh A==; X-IronPort-AV: E=McAfee;i="6500,9779,10457"; a="293360779" X-IronPort-AV: E=Sophos;i="5.93,281,1654585200"; d="scan'208";a="293360779" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Sep 2022 10:40:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,281,1654585200"; d="scan'208";a="680960379" Received: from crojewsk-ctrl.igk.intel.com ([10.102.9.28]) by fmsmga004.fm.intel.com with ESMTP; 01 Sep 2022 10:40:29 -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, andy@kernel.org, linux-kernel@vger.kernel.org, willy@infradead.org, Cezary Rojewski , Andy Shevchenko Subject: [PATCH v3 1/2] lib/string_helpers: Introduce tokenize_user_input() Date: Thu, 1 Sep 2022 19:50:21 +0200 Message-Id: <20220901175022.334824-2-cezary.rojewski@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220901175022.334824-1-cezary.rojewski@intel.com> References: <20220901175022.334824-1-cezary.rojewski@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 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..97583dae556f 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 tokenize_user_input(const char __user *from, size_t count, int **tkns); + #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..f878afccab4c 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); +/** + * tokenize_user_input - 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 tokenize_user_input(const char __user *from, size_t count, int **tkns) +{ + 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); + *tkns = ints; + +free_buf: + kfree(buf); + return ret; +} +EXPORT_SYMBOL(tokenize_user_input); + static bool unescape_space(char **src, char **dst) { char *p = *dst, *q = *src; -- 2.25.1