[PATCH] stdlib: random_r: fix unaligned access in initstate and initstate_r [BZ #30584]

Florian Weimer fweimer@redhat.com
Tue Dec 10 06:47:12 GMT 2024


* Sam James:

> diff --git a/stdlib/Makefile b/stdlib/Makefile
> index 370cfa57aa..715446970f 100644
> --- a/stdlib/Makefile
> +++ b/stdlib/Makefile
> @@ -299,6 +299,7 @@ tests := \

> -      int32_t val = ((state[0] * 1103515245U) + 12345U) & 0x7fffffff;
> -      state[0] = val;
> +      int32_t val = ((read_state(state, 0) * 1103515245U) + 12345U) & 0x7fffffff;

I think this line exceeds 80 characters.

> diff --git a/stdlib/tst-random-bz30584.c b/stdlib/tst-random-bz30584.c
> new file mode 100644
> index 0000000000..2e82dabaf3
> --- /dev/null
> +++ b/stdlib/tst-random-bz30584.c

> +static int
> +do_test (void)
> +{
> +  struct random_data rand_state;
> +  char buf[128 + sizeof (int32_t)];
> +  rand_state.state = NULL;
> +
> +  /* Test initstate_r with an unaligned `state` array. */

Missing space after .  Also in the same comment about initstate.

> +  initstate_r (time (NULL), buf + 1, sizeof buf - 1, &rand_state);

Please stick a union with a double around buf, so that it has to be
aligned.  Not all ABIs guarantee large array alignment.

Looks good otherwise.

Thanks,
Florian



More information about the Libc-alpha mailing list