Tweaks for bug4413 fix

The thing that's limited to 63 bytes is a "label", not a hostname.

Docment input constraints and behavior on bogus inputs.

Generally it's better to check for overflow-like conditions before
than after.  In this case, it's not a true overflow, so we're okay,
but let's be consistent.

pedantic less->fewer in the documentation
This commit is contained in:
Nick Mathewson 2012-01-09 19:14:51 -05:00
parent 3fadc074ca
commit b1ee1a719d

View File

@ -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 <b>prefix</b>,
* ending with <b>suffix</b>, and containing no less than
* ending with <b>suffix</b>, and containing no fewer than
* <b>min_rand_len</b> and no more than <b>max_rand_len</b> random base32
* characters between. */
* characters between.
*
* Clip <b>max_rand_len</b> 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;