for fuzzing: Add a function to make a buf with given contents

(Teor wrote the code, nick extracted it into a smaller patch.)
This commit is contained in:
teor 2016-12-13 19:13:57 -05:00 committed by Nick Mathewson
parent 02068c6391
commit fca91a7793
2 changed files with 51 additions and 0 deletions

View File

@ -281,6 +281,7 @@ buf_pullup(buf_t *buf, size_t bytes)
} }
#ifdef TOR_UNIT_TESTS #ifdef TOR_UNIT_TESTS
/* Return the data from the first chunk of buf in cp, and its length in sz. */
void void
buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz) buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz)
{ {
@ -292,6 +293,53 @@ buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz)
*sz = buf->head->datalen; *sz = buf->head->datalen;
} }
} }
/* Write sz bytes from cp into a newly allocated buffer buf.
* Returns NULL when passed a NULL cp or zero sz.
* Asserts on failure: only for use in unit tests.
* buf must be freed using buf_free(). */
buf_t *
buf_new_with_data(const char *cp, size_t sz)
{
/* Validate arguments */
if (!cp || sz <= 0) {
return NULL;
}
tor_assert(sz < SSIZE_T_CEILING);
/* Allocate a buffer */
buf_t *buf = buf_new_with_capacity(sz);
tor_assert(buf);
assert_buf_ok(buf);
tor_assert(!buf->head);
/* Allocate a chunk that is sz bytes long */
buf->head = chunk_new_with_alloc_size(CHUNK_ALLOC_SIZE(sz));
buf->tail = buf->head;
tor_assert(buf->head);
assert_buf_ok(buf);
tor_assert(buf_allocation(buf) >= sz);
/* Copy the data and size the buffers */
tor_assert(sz <= buf_slack(buf));
tor_assert(sz <= CHUNK_REMAINING_CAPACITY(buf->head));
memcpy(&buf->head->mem[0], cp, sz);
buf->datalen = sz;
buf->head->datalen = sz;
buf->head->data = &buf->head->mem[0];
assert_buf_ok(buf);
/* Make sure everything is large enough */
tor_assert(buf_allocation(buf) >= sz);
tor_assert(buf_allocation(buf) >= buf_datalen(buf) + buf_slack(buf));
/* Does the buffer implementation allocate more than the requested size?
* (for example, by rounding up). If so, these checks will fail. */
tor_assert(buf_datalen(buf) == sz);
tor_assert(buf_slack(buf) == 0);
return buf;
}
#endif #endif
/** Remove the first <b>n</b> bytes from buf. */ /** Remove the first <b>n</b> bytes from buf. */

View File

@ -64,7 +64,10 @@ void assert_buf_ok(buf_t *buf);
#ifdef BUFFERS_PRIVATE #ifdef BUFFERS_PRIVATE
STATIC int buf_find_string_offset(const buf_t *buf, const char *s, size_t n); 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); STATIC void buf_pullup(buf_t *buf, size_t bytes);
#ifdef TOR_UNIT_TESTS
void buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz); void buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz);
buf_t *buf_new_with_data(const char *cp, size_t sz);
#endif
STATIC size_t preferred_chunk_size(size_t target); STATIC size_t preferred_chunk_size(size_t target);
#define DEBUG_CHUNK_ALLOC #define DEBUG_CHUNK_ALLOC