Refactor initiate_descriptor_downloads() to be safer

(It's smarter to use asprintf and join than character pointers and a
long buffer.)
This commit is contained in:
Arlo Breault 2014-09-23 12:21:08 -04:00 committed by Nick Mathewson
parent 29f15a97ed
commit 21d5dbd474
2 changed files with 27 additions and 23 deletions

View File

@ -4289,18 +4289,13 @@ list_pending_fpsk_downloads(fp_pair_map_t *result)
* range.) If <b>source</b> is given, download from <b>source</b>; * range.) If <b>source</b> is given, download from <b>source</b>;
* otherwise, download from an appropriate random directory server. * otherwise, download from an appropriate random directory server.
*/ */
static void MOCK_IMPL(STATIC void, initiate_descriptor_downloads,
initiate_descriptor_downloads(const routerstatus_t *source, (const routerstatus_t *source, int purpose, smartlist_t *digests,
int purpose, int lo, int hi, int pds_flags))
smartlist_t *digests,
int lo, int hi, int pds_flags)
{ {
int i, n = hi-lo;
char *resource, *cp; char *resource, *cp;
size_t r_len;
int digest_len = DIGEST_LEN, enc_digest_len = HEX_DIGEST_LEN; int digest_len = DIGEST_LEN, enc_digest_len = HEX_DIGEST_LEN;
char sep = '+'; char *sep = "+";
int b64_256 = 0; int b64_256 = 0;
if (purpose == DIR_PURPOSE_FETCH_MICRODESC) { if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
@ -4308,32 +4303,37 @@ initiate_descriptor_downloads(const routerstatus_t *source,
* 256-bit digests. */ * 256-bit digests. */
digest_len = DIGEST256_LEN; digest_len = DIGEST256_LEN;
enc_digest_len = BASE64_DIGEST256_LEN; enc_digest_len = BASE64_DIGEST256_LEN;
sep = '-'; sep = "-";
b64_256 = 1; b64_256 = 1;
} }
if (n <= 0)
return;
if (lo < 0) if (lo < 0)
lo = 0; lo = 0;
if (hi > smartlist_len(digests)) if (hi > smartlist_len(digests))
hi = smartlist_len(digests); hi = smartlist_len(digests);
r_len = 8 + (enc_digest_len+1)*n; if (hi-lo <= 0)
cp = resource = tor_malloc(r_len); return;
memcpy(cp, "d/", 2);
cp += 2; digest_len += 1; // for the NULL
for (i = lo; i < hi; ++i) { smartlist_t *tmp = smartlist_new();
for (; lo < hi; ++lo) {
cp = tor_malloc(enc_digest_len);
if (b64_256) { if (b64_256) {
digest256_to_base64(cp, smartlist_get(digests, i)); digest256_to_base64(cp, smartlist_get(digests, lo));
} else { } else {
base16_encode(cp, r_len-(cp-resource), base16_encode(cp, enc_digest_len, smartlist_get(digests, lo), digest_len);
smartlist_get(digests,i), digest_len);
} }
cp += enc_digest_len; smartlist_add(tmp, cp);
*cp++ = sep;
} }
memcpy(cp-1, ".z", 3);
cp = smartlist_join_strings(tmp, sep, 0, NULL);
tor_asprintf(&resource, "d/%s.z", cp);
SMARTLIST_FOREACH(tmp, char *, cp1, tor_free(cp1));
smartlist_free(tmp);
tor_free(cp);
if (source) { if (source) {
/* We know which authority we want. */ /* We know which authority we want. */

View File

@ -211,6 +211,10 @@ STATIC int choose_array_element_by_weight(const u64_dbl_t *entries,
int n_entries); int n_entries);
STATIC void scale_array_elements_to_u64(u64_dbl_t *entries, int n_entries, STATIC void scale_array_elements_to_u64(u64_dbl_t *entries, int n_entries,
uint64_t *total_out); uint64_t *total_out);
MOCK_DECL(STATIC void, initiate_descriptor_downloads,
(const routerstatus_t *source, int purpose, smartlist_t *digests,
int lo, int hi, int pds_flags));
#endif #endif
#endif #endif