[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <20230804143910.15504-2-andriy.shevchenko@linux.intel.com>
Date: Fri, 4 Aug 2023 17:39:07 +0300
From: Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
To: Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
Saravana Kannan <saravanak@...gle.com>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Stephen Boyd <sboyd@...nel.org>,
Dario Binacchi <dario.binacchi@...rulasolutions.com>,
Tony Lindgren <tony@...mide.com>, linux-kernel@...r.kernel.org,
linux-clk@...r.kernel.org, linux-tegra@...r.kernel.org,
linux-omap@...r.kernel.org, linux-hardening@...r.kernel.org
Cc: "Rafael J. Wysocki" <rafael@...nel.org>,
Peter De Schrijver <pdeschrijver@...dia.com>,
Prashant Gaikwad <pgaikwad@...dia.com>,
Michael Turquette <mturquette@...libre.com>,
Thierry Reding <thierry.reding@...il.com>,
Jonathan Hunter <jonathanh@...dia.com>,
Tero Kristo <kristo@...nel.org>,
Kees Cook <keescook@...omium.org>,
Andy Shevchenko <andy@...nel.org>
Subject: [PATCH v2 1/4] lib/string_helpers: Add kstrdup_and_replace() helper
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 <andriy.shevchenko@...ux.intel.com>
---
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
--
2.40.0.1.gaa8946217a0b
Powered by blists - more mailing lists