qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM
@ 2017-07-13 17:32 Peter Maydell
  2017-07-13 18:42 ` Kamil Rytarowski
  2017-07-13 20:16 ` Eric Blake
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Maydell @ 2017-07-13 17:32 UTC (permalink / raw)
  To: QEMU Developers; +Cc: Eric Blake, Markus Armbruster

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

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?
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.

Or we could just have the makefile check whether we have bash,
and if not then set
  RANDOM=4

(thus putting NetBSD into the same boat as all our dash users).

Opinions?

thanks
-- PMM

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM
  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
  1 sibling, 0 replies; 4+ messages in thread
From: Kamil Rytarowski @ 2017-07-13 18:42 UTC (permalink / raw)
  To: qemu-devel

[-- Attachment #1: Type: text/plain, Size: 1067 bytes --]

On 13.07.2017 19:32, 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
> 
> 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?
> 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.
> 
> Or we could just have the makefile check whether we have bash,
> and if not then set
>   RANDOM=4
> 
> (thus putting NetBSD into the same boat as all our dash users).
> 
> Opinions?
> 

From the NetBSD point of view, please skip it. We support $RANDOM in
NetBSD 8.0_BETA.

> thanks
> -- PMM
> 



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM
  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
  2017-07-13 21:15   ` Peter Maydell
  1 sibling, 1 reply; 4+ messages in thread
From: Eric Blake @ 2017-07-13 20:16 UTC (permalink / raw)
  To: Peter Maydell, QEMU Developers; +Cc: Markus Armbruster

[-- 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 --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Qemu-devel] tests/Makefile.include non-portable use of $RANDOM
  2017-07-13 20:16 ` Eric Blake
@ 2017-07-13 21:15   ` Peter Maydell
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Maydell @ 2017-07-13 21:15 UTC (permalink / raw)
  To: Eric Blake; +Cc: QEMU Developers, Markus Armbruster

On 13 July 2017 at 21:16, Eric Blake <eblake@redhat.com> wrote:
> On 07/13/2017 12:32 PM, Peter Maydell wrote:
>> 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.

"has glibc" != "has bash as /bin/sh". In particular most
of my build-test systems run ubuntu and will have dash
for /bin/sh.

On the other hand the :-0 trick is simple and probably
good enough.

thanks
-- PMM

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-07-13 21:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2017-07-13 21:15   ` Peter Maydell

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).