From 1f86b7f5fc4e6671bb0318379612b62aa8944486 Mon Sep 17 00:00:00 2001 From: Krisztian Litkey Date: Thu, 24 Jan 2013 20:05:29 +0200 Subject: [PATCH] common: added fragbuf trim and a check for uninitialized pull. --- src/common/fragbuf.c | 23 +++++++++++++++++++++++ src/common/fragbuf.h | 3 +++ 2 files changed, 26 insertions(+) diff --git a/src/common/fragbuf.c b/src/common/fragbuf.c index df06c76..d95d190 100644 --- a/src/common/fragbuf.c +++ b/src/common/fragbuf.c @@ -115,6 +115,23 @@ void *mrp_fragbuf_alloc(mrp_fragbuf_t *buf, size_t size) } +int mrp_fragbuf_trim(mrp_fragbuf_t *buf, void *ptr, size_t osize, size_t nsize) +{ + size_t diff; + + if (ptr + osize == buf->data + buf->used) { /* looks like the last alloc */ + if (nsize <= osize) { + diff = osize - nsize; + buf->used -= diff; + + return TRUE; + } + } + + return FALSE; +} + + int mrp_fragbuf_push(mrp_fragbuf_t *buf, void *data, size_t size) { void *ptr; @@ -140,6 +157,12 @@ int mrp_fragbuf_pull(mrp_fragbuf_t *buf, void **datap, size_t *sizep) if (buf == NULL || buf->used <= 0) return FALSE; + if (MRP_UNLIKELY(*datap && + (*datap < buf->data || *datap > buf->data + buf->used))) { + mrp_log_warning("%s(): *** looks like we're called with an unreset " + "datap pointer... ***", __FUNCTION__); + } + /* start of iteration */ if (*datap == NULL) { if (!buf->framed) { diff --git a/src/common/fragbuf.h b/src/common/fragbuf.h index 8c62070..2fcf06b 100644 --- a/src/common/fragbuf.h +++ b/src/common/fragbuf.h @@ -46,6 +46,9 @@ void mrp_fragbuf_destroy(mrp_fragbuf_t *buf); /** Allocate a buffer of the given size from the buffer. */ void *mrp_fragbuf_alloc(mrp_fragbuf_t *buf, size_t size); +/** Trim the last allocation to nsize bytes. */ +int mrp_fragbuf_trim(mrp_fragbuf_t *buf, void *ptr, size_t osize, size_t nsize); + /** Append the given data to the buffer. */ int mrp_fragbuf_push(mrp_fragbuf_t *buf, void *data, size_t size); -- 2.7.4