[PATCH 010/437] lib/string_helpers: add parse_int_array_iter()

Jens Axboe
2024-04-11T15:31:46Z

Works just like parse_int_array_user(), except it takes and iov_iter
instead of a ubuf + length.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
include/linux/string_helpers.h | 2 ++
lib/string_helpers.c | 19 +++++++++++++++++++
2 files changed, 21 insertions(+)

diff --git a/include/linux/string_helpers.h b/include/linux/string_helpers.h
index e93fbb5b0c01..f38157114fc0 100644
--- a/include/linux/string_helpers.h
+++ b/include/linux/string_helpers.h
@@ -11,6 +11,7 @@
struct device;
struct file;
struct task_struct;
+struct iov_iter;

static inline bool string_is_terminated(const char *s, int len)
{
@@ -32,6 +33,7 @@ int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
char *buf, int len);

int parse_int_array_user(const char __user *from, size_t count, int **array);
+int parse_int_array_iter(struct iov_iter *from, int **array);

#define UNESCAPE_SPACE BIT(0)
#define UNESCAPE_OCTAL BIT(1)
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 77f942809556..ced4f77dc5e8 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -188,6 +188,25 @@ int parse_int_array_user(const char __user *from, size_t count, int **array)
}
EXPORT_SYMBOL(parse_int_array_user);

+/**
+ * parse_int_array_iter - Split string into a sequence of integers
+ * @from: The iov_iter buffer to read from
+ * @array: Returned pointer to sequence of integers
+ *
+ * See @parse_int_array_user, this is just the iov_iter variant.
+ */
+int parse_int_array_iter(struct iov_iter *from, int **array)
+{
+ char *buf;
+
+ buf = iterdup_nul(from, iov_iter_count(from));
+ if (IS_ERR(buf))
+ return PTR_ERR(buf);
+
+ return __parse_int_array(buf, array);
+}
+EXPORT_SYMBOL(parse_int_array_iter);
+
static bool unescape_space(char **src, char **dst)
{
char *p = *dst, *q = *src;
--
2.43.0