public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 1/2] lib: introduce simple error-checking wrapper for memparse()
@ 2026-01-08 16:52 Dmitry Antipov
  2026-01-08 16:52 ` [PATCH v3 2/2] xfs: adjust handling of a few numerical mount options Dmitry Antipov
                   ` (4 more replies)
  0 siblings, 5 replies; 27+ messages in thread
From: Dmitry Antipov @ 2026-01-08 16:52 UTC (permalink / raw)
  To: Carlos Maiolino, Christoph Hellwig, Kees Cook, Andy Shevchenko,
	Andrew Morton
  Cc: linux-xfs, linux-hardening, Dmitry Antipov

Introduce 'memvalue()' which uses 'memparse()' to parse a string
with optional memory suffix into a non-negative number. If parsing
has succeeded, returns 0 and stores the result at the location
specified by the second argument. Otherwise returns -EINVAL and
leaves the location untouched.

Suggested-by: Christoph Hellwig <hch@infradead.org>
Suggested-by: Kees Cook <kees@kernel.org>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
v3: adjust as suggested by Kees and bump version to match the series
---
 include/linux/string.h |  1 +
 lib/cmdline.c          | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 1b564c36d721..470c7051c58b 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -319,6 +319,7 @@ DEFINE_FREE(argv_free, char **, if (!IS_ERR_OR_NULL(_T)) argv_free(_T))
 extern int get_option(char **str, int *pint);
 extern char *get_options(const char *str, int nints, int *ints);
 extern unsigned long long memparse(const char *ptr, char **retptr);
+extern int __must_check memvalue(const char *ptr, unsigned long long *valptr);
 extern bool parse_option_str(const char *str, const char *option);
 extern char *next_arg(char *args, char **param, char **val);
 
diff --git a/lib/cmdline.c b/lib/cmdline.c
index 90ed997d9570..cf81c6363f6c 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -190,6 +190,32 @@ unsigned long long memparse(const char *ptr, char **retptr)
 }
 EXPORT_SYMBOL(memparse);
 
+/**
+ *	memvalue -  Wrap memparse() with simple error detection
+ *	@ptr: Where parse begins
+ *	@valptr: Where to store result
+ *
+ *	Unconditionally returns -EINVAL for a presumably negative value.
+ *	Otherwise uses memparse() to parse a string into a number stored
+ *	at @valptr and returns 0 or -EINVAL if an unrecognized character
+ *	was encountered. For a non-zero return value, memory at @valptr
+ *	is left untouched.
+ */
+int __must_check memvalue(const char *ptr, unsigned long long *valptr)
+{
+	unsigned long long ret;
+	char *end;
+
+	if (*ptr == '-')
+		return -EINVAL;
+	ret = memparse(ptr, &end);
+	if (*end)
+		return -EINVAL;
+	*valptr = ret;
+	return 0;
+}
+EXPORT_SYMBOL(memvalue);
+
 /**
  *	parse_option_str - Parse a string and check an option is set or not
  *	@str: String to be parsed
-- 
2.52.0


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

end of thread, other threads:[~2026-01-21 13:25 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-08 16:52 [PATCH v3 1/2] lib: introduce simple error-checking wrapper for memparse() Dmitry Antipov
2026-01-08 16:52 ` [PATCH v3 2/2] xfs: adjust handling of a few numerical mount options Dmitry Antipov
2026-01-08 17:06 ` [PATCH v3 1/2] lib: introduce simple error-checking wrapper for memparse() Kees Cook
2026-01-08 17:42   ` Andrew Morton
2026-01-08 17:55     ` Kees Cook
2026-01-09 12:53       ` Carlos Maiolino
2026-01-08 20:05 ` Andy Shevchenko
2026-01-09 11:41   ` Dmitry Antipov
2026-01-09 17:09     ` Andy Shevchenko
2026-01-09 17:11     ` Andy Shevchenko
2026-01-11  0:01       ` Andrew Morton
2026-01-08 20:10 ` Andy Shevchenko
2026-01-09 11:05   ` Dmitry Antipov
2026-01-09 11:18     ` Andy Shevchenko
2026-01-20  0:06 ` Andrew Morton
2026-01-20 14:12   ` [PATCH v4 1/3] " Dmitry Antipov
2026-01-20 14:12     ` [PATCH v4 2/3] lib: fix a few comments to match kernel-doc -Wreturn style Dmitry Antipov
2026-01-20 14:47       ` Andy Shevchenko
2026-01-20 14:12     ` [PATCH v4 3/3] xfs: adjust handling of a few numerical mount options Dmitry Antipov
2026-01-20 14:59       ` Andy Shevchenko
2026-01-20 16:57         ` Dmitry Antipov
2026-01-20 21:49           ` Andy Shevchenko
2026-01-20 22:55             ` Darrick J. Wong
2026-01-21  5:21               ` Dmitry Antipov
2026-01-21 13:25                 ` Andy Shevchenko
2026-01-20 14:45     ` [PATCH v4 1/3] lib: introduce simple error-checking wrapper for memparse() Andy Shevchenko
2026-01-20 14:46       ` Andy Shevchenko

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