From: Nick Mathewson Date: Tue, 21 Apr 2015 15:30:21 +0000 (-0400) Subject: Make the crypto_rand_int_range return value right-exclusive. X-Git-Tag: tor-0.2.7.1-alpha~54^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6bf31543dcda169aa1840fd7e0a0e0ff8a5f9640;p=thirdparty%2Ftor.git Make the crypto_rand_int_range return value right-exclusive. --- diff --git a/src/common/crypto.c b/src/common/crypto.c index 1c4eda90fd..24706ccb96 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -2317,23 +2317,23 @@ crypto_rand_int(unsigned int max) } } -/** Return a pseudorandom integer, chosen uniformly from the values between - * min and max inclusive. +/** Return a pseudorandom integer, chosen uniformly from the values i + * such that min <= i < max. * - * min MUST be between 0 and max - 1. - * max MUST be bigger than min and <= to INT_MAX. + * min MUST be in range [0, max). + * max MUST be in range (min, INT_MAX]. */ int crypto_rand_int_range(unsigned int min, unsigned int max) { - tor_assert(min <= max); + tor_assert(min < max); tor_assert(max <= INT_MAX); /* The overflow is avoided here because crypto_rand_int() returns a value * between 0 and (max - min - 1) with max being <= INT_MAX and min <= max. * This is why we add 1 to the maximum value so we can actually get max as * a return value. */ - return min + crypto_rand_int(max - min + 1); + return min + crypto_rand_int(max - min); } /** Return a pseudorandom 64-bit integer, chosen uniformly from the values @@ -2398,7 +2398,7 @@ crypto_random_hostname(int min_rand_len, int max_rand_len, const char *prefix, if (min_rand_len > max_rand_len) min_rand_len = max_rand_len; - randlen = crypto_rand_int_range(min_rand_len, max_rand_len); + randlen = crypto_rand_int_range(min_rand_len, max_rand_len+1); prefixlen = strlen(prefix); resultlen = prefixlen + strlen(suffix) + randlen + 16;