diff --git a/src/common/crypto.c b/src/common/crypto.c
index 9ee3d989a3..7edc7f228e 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -82,8 +82,8 @@
#include "sha256.c"
#define SHA256_Final(a,b) sha256_done(b,a)
-/* Bug 4413*/
-#define MAX_HOSTNAME_SIZE 63
+/** Longest recognized */
+#define MAX_DNS_LABEL_SIZE 63
static unsigned char *
SHA256(const unsigned char *m, size_t len, unsigned char *d)
@@ -2545,9 +2545,12 @@ crypto_rand_double(void)
}
/** Generate and return a new random hostname starting with prefix,
- * ending with suffix, and containing no less than
+ * ending with suffix, and containing no fewer than
* min_rand_len and no more than max_rand_len random base32
- * characters between. */
+ * characters between.
+ *
+ * Clip max_rand_len to MAX_DNS_LABEL_SIZE.
+ **/
char *
crypto_random_hostname(int min_rand_len, int max_rand_len, const char *prefix,
const char *suffix)
@@ -2556,12 +2559,12 @@ crypto_random_hostname(int min_rand_len, int max_rand_len, const char *prefix,
int randlen, rand_bytes_len;
size_t resultlen, prefixlen;
- tor_assert(max_rand_len >= min_rand_len);
+ if (max_rand_len > MAX_DNS_LABEL_SIZE)
+ max_rand_len = MAX_DNS_LABEL_SIZE;
+ if (min_rand_len > max_rand_len)
+ min_rand_len = max_rand_len;
randlen = min_rand_len + crypto_rand_int(max_rand_len - min_rand_len + 1);
- if (randlen > MAX_HOSTNAME_SIZE) {
- randlen = MAX_HOSTNAME_SIZE;
- }
prefixlen = strlen(prefix);
resultlen = prefixlen + strlen(suffix) + randlen + 16;