[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM |
Date: |
Thu, 13 Jul 2017 15:16:10 -0500 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 |
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
signature.asc
Description: OpenPGP digital signature