Linux Hardening
 help / color / mirror / Atom feed
* [PATCH] lib: introduce simple error-checking wrapper for memparse()
@ 2026-01-07 18:36 Dmitry Antipov
  2026-01-07 18:36 ` [PATCH v2] xfs: adjust handling of a few numerical mount options Dmitry Antipov
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Dmitry Antipov @ 2026-01-07 18:36 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 number and returns this number or ULLONG_MAX
if the number is negative or an unrecognized character was encountered.

Suggested-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 include/linux/string.h |  1 +
 lib/cmdline.c          | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 1b564c36d721..c63bcff820a1 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 unsigned long long memvalue(const char *ptr);
 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..e2455a6d17ff 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -190,6 +190,27 @@ unsigned long long memparse(const char *ptr, char **retptr)
 }
 EXPORT_SYMBOL(memparse);
 
+/**
+ *	memvalue -  Wrap memparse() with simple error detection
+ *	@ptr: Where parse begins
+ *
+ *	Unconditionally returns ULLONG_MAX for a presumably negative value.
+ *	Otherwise uses memparse() to parse a string into a number and returns
+ *	this number or ULLONG_MAX if an unrecognized character was encountered.
+ */
+
+unsigned long long memvalue(const char *ptr)
+{
+	unsigned long long ret;
+	char *end;
+
+	if (*ptr == '-')
+		return ULLONG_MAX;
+	ret = memparse(ptr, &end);
+	return *end ? ULLONG_MAX : ret;
+}
+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] 8+ messages in thread

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

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-07 18:36 [PATCH] lib: introduce simple error-checking wrapper for memparse() Dmitry Antipov
2026-01-07 18:36 ` [PATCH v2] xfs: adjust handling of a few numerical mount options Dmitry Antipov
2026-01-07 18:48 ` [PATCH] lib: introduce simple error-checking wrapper for memparse() Andrew Morton
2026-01-08 13:14   ` Dmitry Antipov
2026-01-07 19:22 ` Andy Shevchenko
2026-01-07 19:24   ` Andy Shevchenko
2026-01-07 20:08 ` Kees Cook
2026-01-08  9:31   ` Christoph Hellwig

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