Re: [PATCH v4 1/3] lib: introduce simple error-checking wrapper for memparse()

Andy Shevchenko
2026-01-20T14:45:40Z

On Tue, Jan 20, 2026 at 05:12:27PM +0300, Dmitry Antipov wrote:
> 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.

...

> +/**
> + * memvalue - Wrap memparse() with simple error detection
> + * @ptr: Where parse begins
> + * @valptr: Where to store result
> + *
> + * Uses memparse() to parse a string into a number stored at
> + * @valptr, leaving memory at @valptr untouched in case of error.
> + *
> + * Return: -EINVAL for a presumably negative value or if an
> + * unrecognized character was encountered, and 0 otherwise.
> + */
> +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;
> +}

My questions seem left unsettled:
- why -EINVAL in the first place and not -ERANGE in the first place;
- why do we need this patch _at all_ based on the how callers are
doing now (w.o. this change), i.o.w. why the memparse() can't be
used directly.

--
With Best Regards,
Andy Shevchenko