public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] lib/string_helpers: drop redundant allocation in kasprintf_strarray
@ 2026-04-15 12:25 Thorsten Blum
  2026-04-15 12:25 ` [PATCH 2/2] lib/string_helpers: annotate struct strarray with __counted_by_ptr Thorsten Blum
  2026-04-15 14:42 ` [PATCH 1/2] lib/string_helpers: drop redundant allocation in kasprintf_strarray Andy Shevchenko
  0 siblings, 2 replies; 6+ messages in thread
From: Thorsten Blum @ 2026-04-15 12:25 UTC (permalink / raw)
  To: Andrew Morton, Kees Cook, Andy Shevchenko
  Cc: Thorsten Blum, linux-hardening, linux-kernel

kasprintf_strarray() returns an array of N strings and kfree_strarray()
also frees N entries.  However, kasprintf_strarray() currently allocates
N+1 char pointers.  Allocate exactly N pointers instead of N+1.

Also update the kernel-doc for @n.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 lib/string_helpers.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 169eaf583494..6a8db441b6fd 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -752,7 +752,7 @@ EXPORT_SYMBOL_GPL(kstrdup_and_replace);
  * kasprintf_strarray - allocate and fill array of sequential strings
  * @gfp: flags for the slab allocator
  * @prefix: prefix to be used
- * @n: amount of lines to be allocated and filled
+ * @n: number of strings to be allocated and filled
  *
  * Allocates and fills @n strings using pattern "%s-%zu", where prefix
  * is provided by caller. The caller is responsible to free them with
@@ -765,7 +765,7 @@ char **kasprintf_strarray(gfp_t gfp, const char *prefix, size_t n)
 	char **names;
 	size_t i;
 
-	names = kcalloc(n + 1, sizeof(char *), gfp);
+	names = kcalloc(n, sizeof(char *), gfp);
 	if (!names)
 		return NULL;
 

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-04-16  7:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-15 12:25 [PATCH 1/2] lib/string_helpers: drop redundant allocation in kasprintf_strarray Thorsten Blum
2026-04-15 12:25 ` [PATCH 2/2] lib/string_helpers: annotate struct strarray with __counted_by_ptr Thorsten Blum
2026-04-15 14:42 ` [PATCH 1/2] lib/string_helpers: drop redundant allocation in kasprintf_strarray Andy Shevchenko
2026-04-15 15:30   ` Thorsten Blum
2026-04-16  7:48     ` Andy Shevchenko
2026-04-15 18:38   ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox