Try to find out early if buffers get trashed or double-freed.

svn:r1225
This commit is contained in:
Nick Mathewson 2004-03-03 22:49:15 +00:00
parent 3e452fed54
commit b7633e2e67
3 changed files with 19 additions and 5 deletions

View File

@ -6,7 +6,9 @@
#include "or.h" #include "or.h"
#define BUFFER_MAGIC 0xB0FFF312u
struct buf_t { struct buf_t {
uint32_t magic; /* for debugging */
char *mem; char *mem;
size_t len; size_t len;
size_t datalen; size_t datalen;
@ -118,6 +120,7 @@ int find_on_inbuf(char *string, int string_len, buf_t *buf) {
buf_t *buf_new_with_capacity(size_t size) { buf_t *buf_new_with_capacity(size_t size) {
buf_t *buf; buf_t *buf;
buf = (buf_t*)tor_malloc(sizeof(buf_t)); buf = (buf_t*)tor_malloc(sizeof(buf_t));
buf->magic = BUFFER_MAGIC;
buf->mem = (char *)tor_malloc(size); buf->mem = (char *)tor_malloc(size);
buf->len = size; buf->len = size;
buf->datalen = 0; buf->datalen = 0;
@ -153,9 +156,10 @@ const char *_buf_peek_raw_buffer(const buf_t *buf)
} }
void buf_free(buf_t *buf) { void buf_free(buf_t *buf) {
assert(buf && buf->mem); assert_buf_ok(buf);
free(buf->mem); buf->magic = 0xDEADBEEF;
free(buf); tor_free(buf->mem);
tor_free(buf);
} }
/* read from socket s, writing onto end of buf. /* read from socket s, writing onto end of buf.
@ -576,6 +580,14 @@ int fetch_from_buf_socks(buf_t *buf, socks_request_t *req) {
} }
} }
void assert_buf_ok(buf_t *buf)
{
assert(buf);
assert(buf->magic == BUFFER_MAGIC);
assert(buf->mem);
assert(buf->datalen <= buf->len);
}
/* /*
Local Variables: Local Variables:
mode:c mode:c

View File

@ -866,8 +866,8 @@ void assert_connection_ok(connection_t *conn, time_t now)
/* buffers */ /* buffers */
if (!connection_is_listener(conn)) { if (!connection_is_listener(conn)) {
assert(conn->inbuf); assert_buf_ok(conn->inbuf);
assert(conn->outbuf); assert_buf_ok(conn->outbuf);
} }
assert(!now || conn->timestamp_lastread <= now); assert(!now || conn->timestamp_lastread <= now);

View File

@ -572,6 +572,8 @@ int fetch_from_buf_http(buf_t *buf,
char **body_out, int max_bodylen); char **body_out, int max_bodylen);
int fetch_from_buf_socks(buf_t *buf, socks_request_t *req); int fetch_from_buf_socks(buf_t *buf, socks_request_t *req);
void assert_buf_ok(buf_t *buf);
/********************************* circuit.c ***************************/ /********************************* circuit.c ***************************/
void circuit_add(circuit_t *circ); void circuit_add(circuit_t *circ);