linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: mszeredi@redhat.com, viro@zeniv.linux.org.uk, jlayton@redhat.com
Cc: dhowells@redhat.com, linux-fsdevel@vger.kernel.org,
	linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 01/14] Provide a function to create a NUL-terminated string from unterminated data [ver #2]
Date: Thu, 11 May 2017 14:59:54 +0100	[thread overview]
Message-ID: <149451119460.4599.10924646742411868045.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <149451118535.4599.16084557087363834548.stgit@warthog.procyon.org.uk>

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 26b6f6a66f83..0c88c0a1a72b 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -123,6 +123,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 656dc5e37a87..be68664bfb73 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

  reply	other threads:[~2017-05-11 14:01 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-11 13:59 [RFC][PATCH 00/14] VFS: Introduce superblock configuration context [ver #2] David Howells
2017-05-11 13:59 ` David Howells [this message]
2017-05-11 14:00 ` [PATCH 02/14] Clean up whitespace in fs/namespace.c " David Howells
2017-05-11 14:00 ` [PATCH 03/14] VFS: Make get_mnt_ns() return the namespace " David Howells
2017-05-11 14:00 ` [PATCH 04/14] VFS: Make get_filesystem() return the affected filesystem " David Howells
2017-05-11 14:00 ` [PATCH 05/14] VFS: Provide empty name qstr " David Howells
2017-05-11 14:00 ` [PATCH 06/14] VFS: Introduce a superblock configuration context " David Howells
2017-05-11 14:00 ` [PATCH 07/14] Implement fsopen() to prepare for a mount " David Howells
2017-05-11 14:01 ` [PATCH 08/14] Implement fsmount() to effect a pre-configured " David Howells
2017-05-11 14:01 ` [PATCH 09/14] Sample program for driving fsopen/fsmount " David Howells
2017-05-11 14:01 ` [PATCH 10/14] procfs: Move proc_fill_super() to fs/proc/root.c " David Howells
2017-05-11 14:01 ` [PATCH 11/14] proc: Add superblock config support to procfs " David Howells
2017-05-11 14:01 ` [PATCH 12/14] NFS: Add mount context support. " David Howells
2017-05-11 14:01 ` [PATCH 13/14] Support legacy filesystems " David Howells
2017-05-11 14:01 ` [PATCH 14/14] Add commands to create or update a superblock " David Howells
2017-05-11 17:26 ` [RFC][PATCH 00/14] VFS: Introduce superblock configuration context " Anna Schumaker
2017-05-11 19:24 ` David Howells
2017-05-12 13:55   ` Anna Schumaker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=149451119460.4599.10924646742411868045.stgit@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=jlayton@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=mszeredi@redhat.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).