[PATCH v2 2/7] Provide __free(argv_free) for argv_split() users

From: Masami Hiramatsu (Google)
Date: Mon Jan 06 2025 - 11:03:40 EST


From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>

Provide __free(argv_free) macro for argv_split() users so that they can
avoid gotos.

Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
Changes in v3:
- Rename to __free(argv_free) so that making it clear to call argv_free().
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..86d5d352068b 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_free, 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);