aboutsummaryrefslogtreecommitdiff
path: root/src/common/memarea.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2010-12-13 18:40:21 -0500
committerNick Mathewson <nickm@torproject.org>2010-12-13 18:40:21 -0500
commit785086cfbaf15a78a921f5589a76517b1d4840b1 (patch)
tree452b47f3da27025aea6193ba045a6e97a0851a65 /src/common/memarea.c
parent649ee99846966c87350cd3282639326f8b4ee4af (diff)
downloadtor-785086cfbaf15a78a921f5589a76517b1d4840b1.tar
tor-785086cfbaf15a78a921f5589a76517b1d4840b1.tar.gz
Have all of our allocation functions and a few others check for underflow
It's all too easy in C to convert an unsigned value to a signed one, which will (on all modern computers) give you a huge signed value. If you have a size_t value of size greater than SSIZE_T_MAX, that is way likelier to be an underflow than it is to be an actual request for more than 2gb of memory in one go. (There's nothing in Tor that should be trying to allocate >2gb chunks.)
Diffstat (limited to 'src/common/memarea.c')
-rw-r--r--src/common/memarea.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/src/common/memarea.c b/src/common/memarea.c
index 9d908bae5..edd7bbe9e 100644
--- a/src/common/memarea.c
+++ b/src/common/memarea.c
@@ -73,6 +73,7 @@ static memarea_chunk_t *freelist = NULL;
static memarea_chunk_t *
alloc_chunk(size_t sz, int freelist_ok)
{
+ tor_assert(sz < SIZE_T_CEILING);
if (freelist && freelist_ok) {
memarea_chunk_t *res = freelist;
freelist = res->next_chunk;
@@ -182,6 +183,7 @@ memarea_alloc(memarea_t *area, size_t sz)
memarea_chunk_t *chunk = area->first;
char *result;
tor_assert(chunk);
+ tor_assert(sz < SIZE_T_CEILING);
if (sz == 0)
sz = 1;
if (chunk->next_mem+sz > chunk->u.mem+chunk->mem_size) {
@@ -240,6 +242,7 @@ memarea_strndup(memarea_t *area, const char *s, size_t n)
size_t ln;
char *result;
const char *cp, *end = s+n;
+ tor_assert(n < SIZE_T_CEILING);
for (cp = s; cp < end && *cp; ++cp)
;
/* cp now points to s+n, or to the 0 in the string. */