summaryrefslogtreecommitdiff
path: root/src/or
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2016-09-13 09:07:12 -0400
committerNick Mathewson <nickm@torproject.org>2016-09-13 09:07:12 -0400
commit20c4b0169493dae7624632dbca49758812138aeb (patch)
treeee4c6ee7fbc6f84f361415b1f34ca35a5cbbc12e /src/or
parent4b182dfc237ba4457b654a0dbc124f721024dab2 (diff)
downloadtor-20c4b0169493dae7624632dbca49758812138aeb.tar.gz
tor-20c4b0169493dae7624632dbca49758812138aeb.zip
Make preferred_chunk_size avoid overflow, handle big inputs better
Also, add tests for the function. Closes 20081; bugfix on 0.2.0.16-alpha. This is a Guido Vranken issue. Thanks, Guido!
Diffstat (limited to 'src/or')
-rw-r--r--src/or/buffers.c5
-rw-r--r--src/or/buffers.h1
2 files changed, 5 insertions, 1 deletions
diff --git a/src/or/buffers.c b/src/or/buffers.c
index 3198572392..c08da63a0d 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -166,9 +166,12 @@ chunk_grow(chunk_t *chunk, size_t sz)
/** Return the allocation size we'd like to use to hold <b>target</b>
* bytes. */
-static inline size_t
+STATIC size_t
preferred_chunk_size(size_t target)
{
+ tor_assert(target <= SIZE_T_CEILING - CHUNK_HEADER_LEN);
+ if (CHUNK_ALLOC_SIZE(target) >= MAX_CHUNK_ALLOC)
+ return CHUNK_ALLOC_SIZE(target);
size_t sz = MIN_CHUNK_ALLOC;
while (CHUNK_SIZE_WITH_ALLOC(sz) < target) {
sz <<= 1;
diff --git a/src/or/buffers.h b/src/or/buffers.h
index 275867c70a..52b21d5885 100644
--- a/src/or/buffers.h
+++ b/src/or/buffers.h
@@ -65,6 +65,7 @@ void assert_buf_ok(buf_t *buf);
STATIC int buf_find_string_offset(const buf_t *buf, const char *s, size_t n);
STATIC void buf_pullup(buf_t *buf, size_t bytes);
void buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz);
+STATIC size_t preferred_chunk_size(size_t target);
#define DEBUG_CHUNK_ALLOC
/** A single chunk on a buffer. */