From: Eric Blake <eblake@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>,
QEMU Developers <qemu-devel@nongnu.org>
Cc: Markus Armbruster <armbru@redhat.com>
Subject: Re: [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM
Date: Thu, 13 Jul 2017 15:16:10 -0500 [thread overview]
Message-ID: <cd8f1049-b975-06d8-528b-9d1c3c4bc0fb@redhat.com> (raw)
In-Reply-To: <CAFEAcA9A0fBB+QE68O7aea+c6SXPaVK4+7B8DJoYE27qshtrcQ@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 1486 bytes --]
On 07/13/2017 12:32 PM, Peter Maydell wrote:
> Currently our test makefile does
>
> MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$((RANDOM % 255 + 1))}
>
> This works on bash, but $RANDOM is bash-specific.
> On dash, it doesn't do what we want, but it does do something:
>
> $ echo $((RANDOM % 255 + 1))
> 1
Being non-random doesn't hurt (it's just less test coverage by default).
>
> On NetBSD the shell complains:
> # echo $((RANDOM % 255 + 1))
> -sh: arith: syntax error: "RANDOM % 255 + 1"
>
> and so 'make check' doesn't work.
>
> Any suggestions for something more portable?
Yes; echo $(( ${RANDOM:-0} % 255 + 1))
should reliably give you 1, even on current BSD /bin/sh, regardless of
whether BSD adds $RANDOM support in the future.
> https://unix.stackexchange.com/questions/140750/generate-random-numbers-in-specific-range
> suggests
> awk 'BEGIN{srand(); print int(1+rand()*256)}'
> and of course there are similar things possible with perl et al.
But why bother. MALLOC_PERTURB_ only makes sense on glibc systems, so
picking a non-random value is no worse on non-glibc systems.
>
> Or we could just have the makefile check whether we have bash,
> and if not then set
> RANDOM=4
Picking that is the same as using ${RANDOM:-4}, without having to
hardcode a check for whether you have bash.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
next prev parent reply other threads:[~2017-07-13 20:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-13 17:32 [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM Peter Maydell
2017-07-13 18:42 ` Kamil Rytarowski
2017-07-13 20:16 ` Eric Blake [this message]
2017-07-13 21:15 ` Peter Maydell
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=cd8f1049-b975-06d8-528b-9d1c3c4bc0fb@redhat.com \
--to=eblake@redhat.com \
--cc=armbru@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/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 NNTP newsgroup(s).