[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH] linux-user: Let user specify random seed
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH] linux-user: Let user specify random seed |
Date: |
Wed, 08 Oct 2014 08:45:07 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.1 |
On 10/08/2014 06:13 AM, Magnus Reftel wrote:
> This patch introduces the -seed command line option and the
> QEMU_RAND_SEED environment variable for setting the random seed, which
> is used for the AT_RANDOM ELF aux entry.
>
> Signed-off-by: Magnus Reftel <address@hidden>
> ---
> linux-user/elfload.c | 1 -
> linux-user/main.c | 21 +++++++++++++++++++++
> 2 files changed, 21 insertions(+), 1 deletion(-)
>
> +++ b/linux-user/main.c
> @@ -46,6 +46,8 @@ unsigned long mmap_min_addr;
> #if defined(CONFIG_USE_GUEST_BASE)
> unsigned long guest_base;
> int have_guest_base;
> +static bool have_rand_seed = false;
static variables are automatically 0-initialized without needing an
explicit initializer.
> +static int rand_seed;
> #if (TARGET_LONG_BITS == 32) && (HOST_LONG_BITS == 64)
> /*
> * When running 32-on-64 we should make sure we can fit all of the possible
> @@ -3546,6 +3548,12 @@ static void handle_arg_pagesize(const char *arg)
> }
> }
>
> +static void handle_arg_randseed(const char *arg)
> +{
> + have_rand_seed = true;
> + rand_seed = atoi(arg);
> +}
atoi() is trash when compared to strtol() - it doesn't diagnose
overflow, trailing garbage, or empty input.
> @@ -3926,6 +3936,17 @@ int main(int argc, char **argv, char **envp)
> do_strace = 1;
> }
>
> + if (getenv("QEMU_RAND_SEED")) {
> + have_rand_seed = true;
> + rand_seed = atoi(getenv("QEMU_RAND_SEED"));
> + }
why not call handle_arg_randseed(getenv("QEMU_RAND_SEED")) here?
> +
> + if (have_rand_seed) {
> + srand(rand_seed);
> + } else {
> + srand((int)time(NULL));
The cast is pointless. This is C.
> + }
Do you even need have_rand_seed? Why not just pre-initialize
rand_seed=time(NULL) and then overwrite rand_seed if the environment
variable is present?
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature