Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm/util: deduplicate code in {kstrdup,kstrndup,kmemdup_nul}

These three functions follow the same pattern. To deduplicate the code,
let's introduce a common helper __kmemdup_nul().

Link: https://lkml.kernel.org/r/20241007144911.27693-7-laoar.shao@gmail.com
Suggested-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
Cc: Simon Horman <horms@kernel.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Alejandro Colomar <alx@kernel.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: David Airlie <airlied@gmail.com>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Eric Paris <eparis@redhat.com>
Cc: James Morris <jmorris@namei.org>
Cc: Jan Kara <jack@suse.cz>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Cc: Matus Jokay <matus.jokay@stuba.sk>
Cc: Maxime Ripard <mripard@kernel.org>
Cc: Ondrej Mosnacek <omosnace@redhat.com>
Cc: Paul Moore <paul@paul-moore.com>
Cc: Quentin Monnet <qmo@kernel.org>
Cc: "Serge E. Hallyn" <serge@hallyn.com>
Cc: Stephen Smalley <stephen.smalley.work@gmail.com>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: Thomas Zimmermann <tzimmermann@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Yafang Shao and committed by
Andrew Morton
43731516 44ff6301

+27 -42
+27 -42
mm/util.c
··· 45 45 EXPORT_SYMBOL(kfree_const); 46 46 47 47 /** 48 + * __kmemdup_nul - Create a NUL-terminated string from @s, which might be unterminated. 49 + * @s: The data to copy 50 + * @len: The size of the data, not including the NUL terminator 51 + * @gfp: the GFP mask used in the kmalloc() call when allocating memory 52 + * 53 + * Return: newly allocated copy of @s with NUL-termination or %NULL in 54 + * case of error 55 + */ 56 + static __always_inline char *__kmemdup_nul(const char *s, size_t len, gfp_t gfp) 57 + { 58 + char *buf; 59 + 60 + /* '+1' for the NUL terminator */ 61 + buf = kmalloc_track_caller(len + 1, gfp); 62 + if (!buf) 63 + return NULL; 64 + 65 + memcpy(buf, s, len); 66 + /* Ensure the buf is always NUL-terminated, regardless of @s. */ 67 + buf[len] = '\0'; 68 + return buf; 69 + } 70 + 71 + /** 48 72 * kstrdup - allocate space for and copy an existing string 49 73 * @s: the string to duplicate 50 74 * @gfp: the GFP mask used in the kmalloc() call when allocating memory ··· 78 54 noinline 79 55 char *kstrdup(const char *s, gfp_t gfp) 80 56 { 81 - size_t len; 82 - char *buf; 83 - 84 - if (!s) 85 - return NULL; 86 - 87 - len = strlen(s) + 1; 88 - buf = kmalloc_track_caller(len, gfp); 89 - if (buf) { 90 - memcpy(buf, s, len); 91 - /* 92 - * During memcpy(), the string might be updated to a new value, 93 - * which could be longer than the string when strlen() is 94 - * called. Therefore, we need to add a NUL terminator. 95 - */ 96 - buf[len - 1] = '\0'; 97 - } 98 - return buf; 57 + return s ? __kmemdup_nul(s, strlen(s), gfp) : NULL; 99 58 } 100 59 EXPORT_SYMBOL(kstrdup); 101 60 ··· 114 107 */ 115 108 char *kstrndup(const char *s, size_t max, gfp_t gfp) 116 109 { 117 - size_t len; 118 - char *buf; 119 - 120 - if (!s) 121 - return NULL; 122 - 123 - len = strnlen(s, max); 124 - buf = kmalloc_track_caller(len+1, gfp); 125 - if (buf) { 126 - memcpy(buf, s, len); 127 - buf[len] = '\0'; 128 - } 129 - return buf; 110 + return s ? __kmemdup_nul(s, strnlen(s, max), gfp) : NULL; 130 111 } 131 112 EXPORT_SYMBOL(kstrndup); 132 113 ··· 188 193 */ 189 194 char *kmemdup_nul(const char *s, size_t len, gfp_t gfp) 190 195 { 191 - char *buf; 192 - 193 - if (!s) 194 - return NULL; 195 - 196 - buf = kmalloc_track_caller(len + 1, gfp); 197 - if (buf) { 198 - memcpy(buf, s, len); 199 - buf[len] = '\0'; 200 - } 201 - return buf; 196 + return s ? __kmemdup_nul(s, len, gfp) : NULL; 202 197 } 203 198 EXPORT_SYMBOL(kmemdup_nul); 204 199