From patchwork Fri Aug 4 14:39:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Shevchenko X-Patchwork-Id: 13341958 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 109C4C05051 for ; Fri, 4 Aug 2023 14:39:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229448AbjHDOjS (ORCPT ); Fri, 4 Aug 2023 10:39:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231796AbjHDOjQ (ORCPT ); Fri, 4 Aug 2023 10:39:16 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 693AC46B1; Fri, 4 Aug 2023 07:39:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1691159955; x=1722695955; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rkrFpsjN1kkwBUnx7S3/fsHajYxtZIFrVprJT5SR/KE=; b=WLS5gvwbycHK2N2dq8jfKBdG6GzBUYrxQRqsvwdSOsaxGVYIUWifCJzr SKv6MGPp9DFpyxeI6ZDmeOJJeNe9C18IGCVoUi72563sIZoxzHGCmKzR5 ZGFT1BZ99bYhrjCEdJwTqkzcZ+oI9rmEqR2BmZ8K4Otwx9VAwJBARhHVe Ya8VTRv2JOfTMqXskBEVZqoGJ4+c/qnthf0792rOHuI2OfpU27pxlAeQq 8pFpPwve/vzoYVijEBjNmgpH1ZsAenkWa7RrNR31q+YKwQfq5P8oViwU+ wqEYxg89S8+W9AkLeBKwo4xXNHl1HugcoXTHNEnQvJhBrgNIsXxneuxmq g==; X-IronPort-AV: E=McAfee;i="6600,9927,10792"; a="373816828" X-IronPort-AV: E=Sophos;i="6.01,255,1684825200"; d="scan'208";a="373816828" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Aug 2023 07:39:14 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10792"; a="723678949" X-IronPort-AV: E=Sophos;i="6.01,255,1684825200"; d="scan'208";a="723678949" Received: from black.fi.intel.com ([10.237.72.28]) by orsmga007.jf.intel.com with ESMTP; 04 Aug 2023 07:39:03 -0700 Received: by black.fi.intel.com (Postfix, from userid 1003) id 6E494BF; Fri, 4 Aug 2023 17:39:14 +0300 (EEST) From: Andy Shevchenko To: Greg Kroah-Hartman , Saravana Kannan , Andy Shevchenko , Stephen Boyd , Dario Binacchi , Tony Lindgren , linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, linux-tegra@vger.kernel.org, linux-omap@vger.kernel.org, linux-hardening@vger.kernel.org Cc: "Rafael J. Wysocki" , Peter De Schrijver , Prashant Gaikwad , Michael Turquette , Thierry Reding , Jonathan Hunter , Tero Kristo , Kees Cook , Andy Shevchenko Subject: [PATCH v2 1/4] lib/string_helpers: Add kstrdup_and_replace() helper Date: Fri, 4 Aug 2023 17:39:07 +0300 Message-Id: <20230804143910.15504-2-andriy.shevchenko@linux.intel.com> X-Mailer: git-send-email 2.40.0.1.gaa8946217a0b In-Reply-To: <20230804143910.15504-1-andriy.shevchenko@linux.intel.com> References: <20230804143910.15504-1-andriy.shevchenko@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-hardening@vger.kernel.org Duplicate a NULL-terminated string and replace all occurrences of the old character with a new one. In other words, provide functionality of kstrdup() + strreplace(). Signed-off-by: Andy Shevchenko --- include/linux/string_helpers.h | 2 ++ lib/string_helpers.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h index 789ab30045da..9d1f5bb74dd5 100644 --- a/include/linux/string_helpers.h +++ b/include/linux/string_helpers.h @@ -109,6 +109,8 @@ char *kstrdup_quotable(const char *src, gfp_t gfp); char *kstrdup_quotable_cmdline(struct task_struct *task, gfp_t gfp); char *kstrdup_quotable_file(struct file *file, gfp_t gfp); +char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp); + char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n); void kfree_strarray(char **array, size_t n); diff --git a/lib/string_helpers.c b/lib/string_helpers.c index d3b1dd718daf..9982344cca34 100644 --- a/lib/string_helpers.c +++ b/lib/string_helpers.c @@ -719,6 +719,21 @@ char *kstrdup_quotable_file(struct file *file, gfp_t gfp) } EXPORT_SYMBOL_GPL(kstrdup_quotable_file); +/* + * Returns duplicate string in which the @old characters are replaced by @new. + */ +char *kstrdup_and_replace(const char *src, char old, char new, gfp_t gfp) +{ + char *dst; + + dst = kstrdup(src, gfp); + if (!dst) + return NULL; + + return strreplace(dst, old, new); +} +EXPORT_SYMBOL_GPL(kstrdup_and_replace); + /** * kasprintf_strarray - allocate and fill array of sequential strings * @gfp: flags for the slab allocator