From: "Arsen Arsenović" <arsen@aarsen.me>
To: Alejandro Colomar <alx.manpages@gmail.com>
Cc: "Alejandro Colomar" <alx@kernel.org>,
"Theo de Raadt" <deraadt@theos.com>,
"Todd C . Miller" <Todd.Miller@sudo.ws>,
"Jason A. Donenfeld" <Jason@zx2c4.com>,
"Cristian Rodríguez" <crrodriguez@opensuse.org>,
"Adhemerval Zanella" <adhemerval.zanella@linaro.org>,
"Yann Droneaud" <ydroneaud@opteya.com>,
"Joseph Myers" <joseph@codesourcery.com>,
libc-alpha@sourceware.org
Subject: Re: [PATCH] Give a useful meaning to arc4random_uniform(0);
Date: Sun, 01 Jan 2023 22:37:46 +0100 [thread overview]
Message-ID: <877cy529vu.fsf@aarsen.me> (raw)
In-Reply-To: <20221231023653.41877-1-alx@kernel.org>
[-- Attachment #1: Type: text/plain, Size: 1794 bytes --]
Hi,
Alejandro Colomar via Libc-alpha <libc-alpha@sourceware.org> writes:
> Special-casing it in the implementation to return 0 was useless.
> Instead, considering 0 as the value after UINT32_MAX has the property
> that it allows implementing the following function without any
> special cases:
>
> uint32_t
> arc4random_range(uint32_t min, uint32_t max)
> {
> return arc4random_uniform(max - min + 1) + min;
> }
>
> This works for any values of min and max (as long as min <= max, of
> course), even for (0, UINT32_MAX).
>
> Oh, and the implementation of arc4random_uniform(3) is now 2 lines
> simpler. :)
While I think this is better than the original meaning, the range [n, n)
cannot produce a (N_0) value. For this reason, I believe the best
behavior for this would be to abort.
Zero is wrong because it's not in [0, 0), which goes also for all values
of [0, 2**32), but not for [0, -1] in the unsigned number space (which
is why I'm giving it more credit than just returning zero), though
specifying that doesn't sit easy with me, since [x, n) and [x, n-1]
aren't necessarily equivalent.
Furthermore, should the need to rely on "return zero for empty range"
behavior arise, an Autoconf test for arc4random_uniform (0) behavior
would be simpler if the test program aborted, as arc4random_uniform (n)
== 0 is quite valid, and easily could happen at configure time.
Should one needs to produce a uniformly distributed value over the full
range of [0, 2**32), they should invoke arc4random (). Moving the
special case into a wrapper function is similarly trivial to achieve
either of the discussed behaviors.
Of course, changing existing APIs is never easy... At least aborts are
easy to spot.
Have a great night.
--
Arsen Arsenović
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 251 bytes --]
next prev parent reply other threads:[~2023-01-01 22:00 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-31 2:36 Alejandro Colomar
2022-12-31 2:48 ` Alejandro Colomar
2022-12-31 2:57 ` Jason A. Donenfeld
2022-12-31 13:39 ` Alejandro Colomar
2022-12-31 8:50 ` Theo de Raadt
2022-12-31 8:51 ` Theo de Raadt
2022-12-31 14:56 ` Alejandro Colomar
2022-12-31 15:13 ` Alejandro Colomar
2022-12-31 15:17 ` Alejandro Colomar
2022-12-31 15:59 ` Alejandro Colomar
2022-12-31 16:03 ` Alejandro Colomar
2023-01-01 8:41 ` Theo de Raadt
2022-12-31 23:07 ` Damien Miller
2022-12-31 23:58 ` Alejandro Colomar
2023-01-01 7:48 ` Ariadne Conill
2023-01-01 9:21 ` Otto Moerbeek
2023-01-01 14:05 ` Alejandro Colomar
2023-01-01 8:34 ` Theo de Raadt
2023-01-01 21:37 ` Arsen Arsenović [this message]
2023-01-01 23:50 ` Alejandro Colomar
2023-01-02 0:02 ` Arsen Arsenović
2023-01-02 11:24 ` Alejandro Colomar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=877cy529vu.fsf@aarsen.me \
--to=arsen@aarsen.me \
--cc=Jason@zx2c4.com \
--cc=Todd.Miller@sudo.ws \
--cc=adhemerval.zanella@linaro.org \
--cc=alx.manpages@gmail.com \
--cc=alx@kernel.org \
--cc=crrodriguez@opensuse.org \
--cc=deraadt@theos.com \
--cc=joseph@codesourcery.com \
--cc=libc-alpha@sourceware.org \
--cc=ydroneaud@opteya.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).