[PATCH v2 2/6] Provide __free(argv) for argv_split() users
From: Masami Hiramatsu (Google)
Date: Sun Jan 05 2025 - 07:48:08 EST
From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
Provide __free(argv) macro for argv_split() users so that they can
avoid gotos.
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
Changes in v2:
- Fix to call argv_free() only if the argument is !IS_ERR_OR_NULL().
- Add including cleanup.h.
---
include/linux/string.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h
index 493ac4862c77..07f2a90d5d9c 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -4,6 +4,7 @@
#include <linux/args.h>
#include <linux/array_size.h>
+#include <linux/cleanup.h> /* for DEFINE_FREE() */
#include <linux/compiler.h> /* for inline */
#include <linux/types.h> /* for size_t */
#include <linux/stddef.h> /* for NULL */
@@ -312,6 +313,8 @@ extern void *kmemdup_array(const void *src, size_t count, size_t element_size, g
extern char **argv_split(gfp_t gfp, const char *str, int *argcp);
extern void argv_free(char **argv);
+DEFINE_FREE(argv, char **, if (!IS_ERR_OR_NULL(_T)) argv_free(_T))
+
/* lib/cmdline.c */
extern int get_option(char **str, int *pint);
extern char *get_options(const char *str, int nints, int *ints);