On Mon, Feb 02, 2026 at 02:54:48PM +0300, Dmitry Antipov wrote:
> In '_parse_integer_limit()', adjust native integer arithmetic
> with near-to-overflow branch where 'check_mul_overflow()' and
> 'check_add_overflow()' are used to check whether an intermediate
> result goes out of range, and denote such a case with ULLONG_MAX,
> thus making the function more similar to standard C library's
> 'strtoull()'. Adjust comment to kernel-doc style as well.

This version is good enough, we may consider further optimisations later on.

Reviewed-by: Andy Shevchenko <[email protected]>

...

>               /*
> -              * Check for overflow only if we are within range of
> -              * it in the max base we support (16)
> +              * Accumulate result if no overflow detected.
> +              * Otherwise just consume valid characters.
>                */
> -             if (unlikely(res & (~0ull << 60))) {
> -                     if (res > div_u64(ULLONG_MAX - val, base))
> -                             rv |= KSTRTOX_OVERFLOW;
> +             if (likely(res != ULLONG_MAX)) {
> +                     if (unlikely(res & (~0ull << 60))) {
> +                             /* We're close to possible overflow. */
> +                             if (check_mul_overflow(res, base, &tmp) ||
> +                                 check_add_overflow(tmp, val, &res)) {
> +                                     res = ULLONG_MAX;
> +                                     rv |= KSTRTOX_OVERFLOW;
> +                             }
> +                     } else
> +                             res = res * base + val;

Formally this should be } else { ... }, but I'm not going to insist.

>               }
> -             res = res * base + val;
>               rv++;
>               s++;

-- 
With Best Regards,
Andy Shevchenko



Reply via email to