[tor-commits] [tor/master] Refactor initiate_descriptor_downloads() to be safer
nickm at torproject.org
nickm at torproject.org
Fri Jan 23 15:03:15 UTC 2015
commit 21d5dbd474d5dad10a2bfa800df078f7fdc8c40b
Author: Arlo Breault <arlolra at gmail.com>
Date: Tue Sep 23 12:21:08 2014 -0400
Refactor initiate_descriptor_downloads() to be safer
(It's smarter to use asprintf and join than character pointers and a
long buffer.)
---
src/or/routerlist.c | 46 +++++++++++++++++++++++-----------------------
src/or/routerlist.h | 4 ++++
2 files changed, 27 insertions(+), 23 deletions(-)
diff --git a/src/or/routerlist.c b/src/or/routerlist.c
index 2fe007d..96814ca 100644
--- a/src/or/routerlist.c
+++ b/src/or/routerlist.c
@@ -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>;
* otherwise, download from an appropriate random directory server.
*/
-static void
-initiate_descriptor_downloads(const routerstatus_t *source,
- int purpose,
- smartlist_t *digests,
- int lo, int hi, int pds_flags)
+MOCK_IMPL(STATIC void, initiate_descriptor_downloads,
+ (const routerstatus_t *source, int purpose, smartlist_t *digests,
+ int lo, int hi, int pds_flags))
{
- int i, n = hi-lo;
char *resource, *cp;
- size_t r_len;
-
int digest_len = DIGEST_LEN, enc_digest_len = HEX_DIGEST_LEN;
- char sep = '+';
+ char *sep = "+";
int b64_256 = 0;
if (purpose == DIR_PURPOSE_FETCH_MICRODESC) {
@@ -4308,32 +4303,37 @@ initiate_descriptor_downloads(const routerstatus_t *source,
* 256-bit digests. */
digest_len = DIGEST256_LEN;
enc_digest_len = BASE64_DIGEST256_LEN;
- sep = '-';
+ sep = "-";
b64_256 = 1;
}
- if (n <= 0)
- return;
if (lo < 0)
lo = 0;
if (hi > smartlist_len(digests))
hi = smartlist_len(digests);
- r_len = 8 + (enc_digest_len+1)*n;
- cp = resource = tor_malloc(r_len);
- memcpy(cp, "d/", 2);
- cp += 2;
- for (i = lo; i < hi; ++i) {
+ if (hi-lo <= 0)
+ return;
+
+ digest_len += 1; // for the NULL
+ smartlist_t *tmp = smartlist_new();
+
+ for (; lo < hi; ++lo) {
+ cp = tor_malloc(enc_digest_len);
if (b64_256) {
- digest256_to_base64(cp, smartlist_get(digests, i));
+ digest256_to_base64(cp, smartlist_get(digests, lo));
} else {
- base16_encode(cp, r_len-(cp-resource),
- smartlist_get(digests,i), digest_len);
+ base16_encode(cp, enc_digest_len, smartlist_get(digests, lo), digest_len);
}
- cp += enc_digest_len;
- *cp++ = sep;
+ smartlist_add(tmp, cp);
}
- 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) {
/* We know which authority we want. */
diff --git a/src/or/routerlist.h b/src/or/routerlist.h
index cfa8683..1e8b7c9 100644
--- a/src/or/routerlist.h
+++ b/src/or/routerlist.h
@@ -211,6 +211,10 @@ STATIC int choose_array_element_by_weight(const 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);
+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
More information about the tor-commits
mailing list