[PATCH v5 2/5] lib: fix memparse() to handle overflow

Dmitry Antipov posted 5 patches 5 days, 17 hours ago
There is a newer version of this series
[PATCH v5 2/5] lib: fix memparse() to handle overflow
Posted by Dmitry Antipov 5 days, 17 hours ago
Since '_parse_integer_limit()' (and so 'simple_strtoull()') is now
capable to handle overflow, adjust 'memparse()' to handle overflow
(denoted by ULLONG_MAX) returned from 'simple_strtoull()'. Also
use 'check_shl_overflow()' to catch an overflow possibly caused
by processing size suffix and denote it with ULLONG_MAX as well.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
v5: initial version to join the series
---
 lib/cmdline.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/cmdline.c b/lib/cmdline.c
index 90ed997d9570..e18fdfb80ba5 100644
--- a/lib/cmdline.c
+++ b/lib/cmdline.c
@@ -151,38 +151,48 @@ unsigned long long memparse(const char *ptr, char **retptr)
 {
 	char *endptr;	/* local pointer to end of parsed string */
 
+	unsigned int shl = 0;
 	unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
 
+	/* Consume valid suffix even in case of overflow. */
 	switch (*endptr) {
 	case 'E':
 	case 'e':
-		ret <<= 10;
+		shl += 10;
 		fallthrough;
 	case 'P':
 	case 'p':
-		ret <<= 10;
+		shl += 10;
 		fallthrough;
 	case 'T':
 	case 't':
-		ret <<= 10;
+		shl += 10;
 		fallthrough;
 	case 'G':
 	case 'g':
-		ret <<= 10;
+		shl += 10;
 		fallthrough;
 	case 'M':
 	case 'm':
-		ret <<= 10;
+		shl += 10;
 		fallthrough;
 	case 'K':
 	case 'k':
-		ret <<= 10;
+		shl += 10;
 		endptr++;
 		fallthrough;
 	default:
 		break;
 	}
 
+	/* If no overflow, apply suffix if any. */
+	if (likely(ret != ULLONG_MAX) && shl) {
+		unsigned long long val;
+
+		ret = (unlikely(check_shl_overflow(ret, shl, &val))
+		       ? ULLONG_MAX : val);
+	}
+
 	if (retptr)
 		*retptr = endptr;
 
-- 
2.52.0
Re: [PATCH v5 2/5] lib: fix memparse() to handle overflow
Posted by Andy Shevchenko 5 days, 16 hours ago
On Wed, Feb 04, 2026 at 04:57:14PM +0300, Dmitry Antipov wrote:
> Since '_parse_integer_limit()' (and so 'simple_strtoull()') is now
> capable to handle overflow, adjust 'memparse()' to handle overflow
> (denoted by ULLONG_MAX) returned from 'simple_strtoull()'. Also
> use 'check_shl_overflow()' to catch an overflow possibly caused
> by processing size suffix and denote it with ULLONG_MAX as well.

Do we have already test cases to cover this?

...

> unsigned long long memparse(const char *ptr, char **retptr)
>  {
>  	char *endptr;	/* local pointer to end of parsed string */

>  

Shouldn't be an empty line in the definition block.

> +	unsigned int shl = 0;
>  	unsigned long long ret = simple_strtoull(ptr, &endptr, 0);

and it would be better to preserve reversed xmas tree order (to some extent).

	char *endptr;	/* local pointer to end of parsed string */
	unsigned long long ret = simple_strtoull(ptr, &endptr, 0);
	unsigned int shl = 0;

> +	/* Consume valid suffix even in case of overflow. */
>  	switch (*endptr) {
>  	case 'E':
>  	case 'e':
> -		ret <<= 10;
> +		shl += 10;
>  		fallthrough;
>  	case 'P':
>  	case 'p':
> -		ret <<= 10;
> +		shl += 10;
>  		fallthrough;
>  	case 'T':
>  	case 't':
> -		ret <<= 10;
> +		shl += 10;
>  		fallthrough;
>  	case 'G':
>  	case 'g':
> -		ret <<= 10;
> +		shl += 10;
>  		fallthrough;
>  	case 'M':
>  	case 'm':
> -		ret <<= 10;
> +		shl += 10;
>  		fallthrough;
>  	case 'K':
>  	case 'k':
> -		ret <<= 10;
> +		shl += 10;
>  		endptr++;
>  		fallthrough;
>  	default:
>  		break;
>  	}

> +	/* If no overflow, apply suffix if any. */
> +	if (likely(ret != ULLONG_MAX) && shl) {

Do we need to check for shl? Yes, it will be an additional check below,
but do we care?

> +		unsigned long long val;

> +		ret = (unlikely(check_shl_overflow(ret, shl, &val))
> +		       ? ULLONG_MAX : val);

Unneeded parentheses, and ? should be on the previous line.
With that said, I prefer to see the regular conditional instead:

		ret = check_shl_overflow(...);
		if (unlikely(ret))
			ret = ...
		else
			ret = val;

> +	}
> +
>  	if (retptr)
>  		*retptr = endptr;

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v5 2/5] lib: fix memparse() to handle overflow
Posted by Dmitry Antipov 4 days, 21 hours ago
On Wed, 2026-02-04 at 16:42 +0200, Andy Shevchenko wrote:

> On Wed, Feb 04, 2026 at 04:57:14PM +0300, Dmitry Antipov wrote:
> > Since '_parse_integer_limit()' (and so 'simple_strtoull()') is now
> > capable to handle overflow, adjust 'memparse()' to handle overflow
> > (denoted by ULLONG_MAX) returned from 'simple_strtoull()'. Also
> > use 'check_shl_overflow()' to catch an overflow possibly caused
> > by processing size suffix and denote it with ULLONG_MAX as well.
> 
> Do we have already test cases to cover this?

In

static const struct cmdline_test_memparse_entry testdata[] = {
        ...
        { "1111111111111111111T",       "",     ULLONG_MAX },
        ...
};

the whole string is valid and so should be recognized by memparse(). Next,
1111111111111111111 fits unsigned long long but 1111111111111111111 << 40
is too large and should be catched by check_shl_overflow().

Dmitry
Re: [PATCH v5 2/5] lib: fix memparse() to handle overflow
Posted by Andy Shevchenko 4 days, 14 hours ago
On Thu, Feb 05, 2026 at 12:17:16PM +0300, Dmitry Antipov wrote:
> On Wed, 2026-02-04 at 16:42 +0200, Andy Shevchenko wrote:
> > On Wed, Feb 04, 2026 at 04:57:14PM +0300, Dmitry Antipov wrote:
> > > Since '_parse_integer_limit()' (and so 'simple_strtoull()') is now
> > > capable to handle overflow, adjust 'memparse()' to handle overflow
> > > (denoted by ULLONG_MAX) returned from 'simple_strtoull()'. Also
> > > use 'check_shl_overflow()' to catch an overflow possibly caused
> > > by processing size suffix and denote it with ULLONG_MAX as well.
> > 
> > Do we have already test cases to cover this?
> 
> In
> 
> static const struct cmdline_test_memparse_entry testdata[] = {
>         ...
>         { "1111111111111111111T",       "",     ULLONG_MAX },
>         ...
> };
> 
> the whole string is valid and so should be recognized by memparse(). Next,
> 1111111111111111111 fits unsigned long long but 1111111111111111111 << 40
> is too large and should be catched by check_shl_overflow().

But if there is a case already, how does it pass?

My understanding is that if we modify the code behaviour the test cases should
be amended at the same time. I guess I missed something here.

-- 
With Best Regards,
Andy Shevchenko