util: Don't overflow in virRandomBits

The function is supposed to return up to 64bit long integer. In
order to do that it calls virRandomBytes() to fill the integer
with random bytes and then masks out everything but requested
bits. However, when doing that it shifts 1U and not 1ULL. So
effectively, requesting 32 random bis or more always return 0
which is not random enough.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Pino Toscano <ptoscano@redhat.com>
This commit is contained in:
Michal Privoznik 2018-08-01 13:26:46 +02:00
parent 3251fc9c9b
commit 78c47a92ec
1 changed files with 1 additions and 1 deletions

View File

@ -68,7 +68,7 @@ uint64_t virRandomBits(int nbits)
return 0;
}
ret &= (1U << nbits) - 1;
ret &= (1ULL << nbits) - 1;
return ret;
}