summaryrefslogtreecommitdiff
path: root/src/or/buffers.c
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/buffers.c
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/buffers.c')
-rw-r--r--src/or/buffers.c5
1 files changed, 4 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;