Re: [PATCH v4 2/6] treewide: use prandom_u32_max() when possible

From: Jason A. Donenfeld
Date: Sat Oct 08 2022 - 18:20:22 EST


On Sat, Oct 08, 2022 at 10:08:03PM +0000, David Laight wrote:
> From: Jason A. Donenfeld
> > Sent: 07 October 2022 19:01
> >
> > Rather than incurring a division or requesting too many random bytes for
> > the given range, use the prandom_u32_max() function, which only takes
> > the minimum required bytes from the RNG and avoids divisions.
> >
> ...
> > --- a/lib/cmdline_kunit.c
> > +++ b/lib/cmdline_kunit.c
> > @@ -76,7 +76,7 @@ static void cmdline_test_lead_int(struct kunit *test)
> > int rc = cmdline_test_values[i];
> > int offset;
> >
> > - sprintf(in, "%u%s", prandom_u32_max(256), str);
> > + sprintf(in, "%u%s", get_random_int() % 256, str);
> > /* Only first '-' after the number will advance the pointer */
> > offset = strlen(in) - strlen(str) + !!(rc == 2);
> > cmdline_do_one_test(test, in, rc, offset);
> > @@ -94,7 +94,7 @@ static void cmdline_test_tail_int(struct kunit *test)
> > int rc = strcmp(str, "") ? (strcmp(str, "-") ? 0 : 1) : 1;
> > int offset;
> >
> > - sprintf(in, "%s%u", str, prandom_u32_max(256));
> > + sprintf(in, "%s%u", str, get_random_int() % 256);
> > /*
> > * Only first and leading '-' not followed by integer
> > * will advance the pointer.
>
> Something has gone backwards here....
> And get_random_u8() looks a better fit.

Wrong patch version.

>
> David
>
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)