[parted-devel] generate_random_id() fix
H. Peter Anvin
hpa at zytor.com
Wed Jul 11 20:52:15 UTC 2007
Jim Meyering wrote:
>
> Thanks for the patch.
> It's definitely an improvement.
>
> However, since there's already code to generate a 32-bit
> serial number in fs/fat/fat.c:
>
> /* hack: use the ext2 uuid library to generate a reasonably random (hopefully
> * with /dev/random) number. Unfortunately, we can only use 4 bytes of it
> */
> static uint32_t
> _gen_new_serial_number ()
> {
> uuid_t uuid;
>
> uuid_generate (uuid);
> return * (uint32_t*) &uuid [0];
> }
>
> I'm thinking about using something like this from both places:
>
> static uint32_t
> _gen_new_serial_number (void)
> {
> static uuid_t uuid;
> static unsigned int i = 0;
> unsigned int n = sizeof uuid / sizeof (uint32_t);
>
> if (i % n == 0)
> {
> uuid_generate (uuid);
> i = 0;
> }
>
> return ((uint32_t *)uuid)[i++];
> }
>
> What do you think?
I would avoid using the UUID, since the UUID is not guaranteed to be
random in any way -- in fact, even for a "random" UUID there are at
least 6 bits which are fixed.
I don't know exactly what uuid_generate() does, so it's hard to say
exactly if this is a problem or not. At the very least I would XOR the
different words of the UUID together.
-hpa
More information about the parted-devel
mailing list