Re-enable buffer RAM guard values.

svn:r6653
This commit is contained in:
Nick Mathewson 2006-06-18 15:53:54 +00:00
parent 11c2e97d51
commit a9cffd2ff0

View File

@ -15,8 +15,7 @@ const char buffers_c_id[] =
#include "or.h" #include "or.h"
/* XXXX Make sentinels work again. */ #define SENTINELS
#undef SENTINELS
#undef CHECK_AFTER_RESIZE #undef CHECK_AFTER_RESIZE
#undef PARANOIA #undef PARANOIA
#undef NOINLINE #undef NOINLINE
@ -59,6 +58,7 @@ struct buf_t {
char *cur; /**< The first byte used for storing data in the buffer. */ char *cur; /**< The first byte used for storing data in the buffer. */
size_t highwater; /**< Largest observed datalen since last buf_shrink */ size_t highwater; /**< Largest observed datalen since last buf_shrink */
size_t len; /**< Maximum amount of data that <b>mem</b> can hold. */ size_t len; /**< Maximum amount of data that <b>mem</b> can hold. */
size_t memsize; /**< DOCDOC */
size_t datalen; /**< Number of bytes currently in <b>mem</b>. */ size_t datalen; /**< Number of bytes currently in <b>mem</b>. */
}; };
@ -89,13 +89,14 @@ buf_normalize(buf_t *buf)
char *newmem, *oldmem; char *newmem, *oldmem;
size_t sz = (buf->mem+buf->len)-buf->cur; size_t sz = (buf->mem+buf->len)-buf->cur;
log_warn(LD_BUG, "Unexpected non-normalized buffer."); log_warn(LD_BUG, "Unexpected non-normalized buffer.");
newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(buf->len))); newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(buf->memsize)));
SET_GUARDS(newmem, buf->len); SET_GUARDS(newmem, buf->memsize);
memcpy(newmem, buf->cur, sz); memcpy(newmem, buf->cur, sz);
memcpy(newmem+sz, buf->mem, buf->datalen-sz); memcpy(newmem+sz, buf->mem, buf->datalen-sz);
oldmem = RAW_MEM(buf->mem); oldmem = RAW_MEM(buf->mem);
tor_free(oldmem); /* Can't use tor_free directly. */ tor_free(oldmem); /* Can't use tor_free directly. */
buf->mem = buf->cur = newmem; buf->mem = buf->cur = newmem;
buf->len = buf->memsize;
check(); check();
} }
} }
@ -230,7 +231,7 @@ buf_resize(buf_t *buf, size_t new_capacity)
buf->len-offset); buf->len-offset);
buf->cur += new_capacity-buf->len; buf->cur += new_capacity-buf->len;
} }
buf->len = new_capacity; buf->memsize = buf->len = new_capacity;
#ifdef CHECK_AFTER_RESIZE #ifdef CHECK_AFTER_RESIZE
assert_buf_ok(buf); assert_buf_ok(buf);
@ -329,7 +330,7 @@ buf_new_with_capacity(size_t size)
buf->magic = BUFFER_MAGIC; buf->magic = BUFFER_MAGIC;
buf->cur = buf->mem = GUARDED_MEM(tor_malloc(ALLOC_LEN(size))); buf->cur = buf->mem = GUARDED_MEM(tor_malloc(ALLOC_LEN(size)));
SET_GUARDS(buf->mem, size); SET_GUARDS(buf->mem, size);
buf->len = size; buf->len = buf->memsize = size;
buf_total_alloc += size; buf_total_alloc += size;
assert_buf_ok(buf); assert_buf_ok(buf);
@ -350,6 +351,7 @@ buf_clear(buf_t *buf)
buf_total_used -= buf->datalen; buf_total_used -= buf->datalen;
buf->datalen = 0; buf->datalen = 0;
buf->cur = buf->mem; buf->cur = buf->mem;
buf->len = buf->memsize;
} }
/** Return the number of bytes stored in <b>buf</b> */ /** Return the number of bytes stored in <b>buf</b> */
@ -1356,7 +1358,7 @@ assert_buf_ok(buf_t *buf)
{ {
uint32_t u32 = get_uint32(buf->mem - 4); uint32_t u32 = get_uint32(buf->mem - 4);
tor_assert(u32 == START_MAGIC); tor_assert(u32 == START_MAGIC);
u32 = get_uint32(buf->mem + buf->len); u32 = get_uint32(buf->mem + buf->memsize);
tor_assert(u32 == END_MAGIC); tor_assert(u32 == END_MAGIC);
} }
#endif #endif