[Devel] [PATCH RHEL7 COMMIT] ms/Provide a function to create a NUL-terminated string from unterminated data

Vasily Averin vvs at virtuozzo.com
Tue Jan 25 12:37:49 MSK 2022


The commit is pushed to "branch-rh7-3.10.0-1160.53.1.vz7.185.x-ovz" and will appear at https://src.openvz.org/scm/ovz/vzkernel.git
after rh7-3.10.0-1160.53.1.vz7.185.2
------>
commit e5a64584acf6ea85ae5bc55d77205fd69fdf41ec
Author: David Howells <dhowells at redhat.com>
Date:   Tue Jan 25 12:37:47 2022 +0300

    ms/Provide a function to create a NUL-terminated string from unterminated data
    
    Provide a function, kmemdup_nul(), that will create a NUL-terminated string
    from an unterminated character array where the length is known in advance.
    
    This is better than kstrndup() in situations where we already know the
    string length as the strnlen() in kstrndup() is superfluous.
    
    Signed-off-by: David Howells <dhowells at redhat.com>
    Signed-off-by: Al Viro <viro at zeniv.linux.org.uk>
    
    (cherry-picked from ms commit f35157417215ec138c920320c746fdb3e04ef1d5)
    https://jira.sw.ru/browse/PSBM-137154
    Signed-off-by: Pavel Tikhomirov <ptikhomirov at virtuozzo.com>
---
 include/linux/string.h |  1 +
 mm/util.c              | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 8d690b3..75ba99d9 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -170,6 +170,7 @@ extern char *kstrdup(const char *s, gfp_t gfp);
 extern const char *kstrdup_const(const char *s, gfp_t gfp);
 extern char *kstrndup(const char *s, size_t len, gfp_t gfp);
 extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
+extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp);
 
 extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
 extern void argv_free(char **argv);
diff --git a/mm/util.c b/mm/util.c
index 6b1cf76..a13fced 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -80,6 +80,8 @@ EXPORT_SYMBOL(kstrdup_const);
  * @s: the string to duplicate
  * @max: read at most @max chars from @s
  * @gfp: the GFP mask used in the kmalloc() call when allocating memory
+ *
+ * Note: Use kmemdup_nul() instead if the size is known exactly.
  */
 char *kstrndup(const char *s, size_t max, gfp_t gfp)
 {
@@ -118,6 +120,28 @@ void *kmemdup(const void *src, size_t len, gfp_t gfp)
 EXPORT_SYMBOL(kmemdup);
 
 /**
+ * kmemdup_nul - Create a NUL-terminated string from unterminated data
+ * @s: The data to stringify
+ * @len: The size of the data
+ * @gfp: the GFP mask used in the kmalloc() call when allocating memory
+ */
+char *kmemdup_nul(const char *s, size_t len, gfp_t gfp)
+{
+	char *buf;
+
+	if (!s)
+		return NULL;
+
+	buf = kmalloc_track_caller(len + 1, gfp);
+	if (buf) {
+		memcpy(buf, s, len);
+		buf[len] = '\0';
+	}
+	return buf;
+}
+EXPORT_SYMBOL(kmemdup_nul);
+
+/**
  * memdup_user - duplicate memory region from user space
  *
  * @src: source address in user space


More information about the Devel mailing list