Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1647078

[PATCH 01/23] Provide a function to create a NUL-terminated string from unterminated data [ver #4]

From David Howells <dhowells@redhat.com>
Newsgroups linux.kernel
Subject [PATCH 01/23] Provide a function to create a NUL-terminated string from unterminated data [ver #4]
Date 2017-05-22 18:00 +0200
Message-ID <tJXUv-7yP-61@gated-at.bofh.it> (permalink)
References <tJXUu-7yP-3@gated-at.bofh.it>
Organization Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903

Show all headers | View raw


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@redhat.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 537918f8a98e..3dd944cfe171 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -131,6 +131,7 @@ extern char *kstrdup(const char *s, gfp_t gfp) __malloc;
 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 464df3489903..cc9f0a2810bd 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -83,6 +83,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)
 {
@@ -121,6 +123,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

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[RFC][PATCH 00/23] VFS: Introduce superblock configuration context  [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 14/23] procfs: Move proc_fill_super() to fs/proc/root.c [ver  #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 19/23] NFS: Split nfs_parse_mount_options() [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 12/23] VFS: Implement fsmount() to effect a pre-configured  mount [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 07/23] VFS: Introduce the structs and doc for a superblock  configuration context [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 17/23] NFS: Constify mount argument match tables [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 09/23] VFS: Implement a superblock configuration context  [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 08/23] VFS: Add LSM hooks for superblock configuration  context [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 21/23] NFS: Add a small buffer in nfs_sb_config to avoid  string dup [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 01/23] Provide a function to create a NUL-terminated string  from unterminated data [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 11/23] VFS: Implement fsopen() to prepare for a mount [ver  #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 05/23] VFS: Provide empty name qstr [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 22/23] NFS: Do some tidying of the parsing code [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 10/23] VFS: Remove unused code after superblock config  context changes [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 03/23] VFS: Make get_mnt_ns() return the namespace [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 13/23] VFS: Add a sample program for fsopen/fsmount [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:00 +0200
  [PATCH 06/23] Provide supplementary error message facility [ver #4] David Howells <dhowells@redhat.com> - 2017-05-22 18:10 +0200

csiph-web