summaryrefslogtreecommitdiff
path: root/printbuf.c
diff options
context:
space:
mode:
authorEric Haszlakiewicz <erh+git@nimenees.com>2012-04-03 14:54:25 -0500
committerEric Haszlakiewicz <erh+git@nimenees.com>2012-04-03 14:54:25 -0500
commit0d79b534568f609a9444c3466ae1f5a2864f702f (patch)
tree824955508602282e93b8138fda928e0e6d02bf49 /printbuf.c
parent61a154e58b0a13f4d9e4cfb74bd1a8b9655baa3d (diff)
downloadjson-c-0d79b534568f609a9444c3466ae1f5a2864f702f.tar.gz
Fix some bugs with how buffer sizes were being calcuated in printbuf_memset and an off-by-one error in printbuf_memappend.
Diffstat (limited to 'printbuf.c')
-rw-r--r--printbuf.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/printbuf.c b/printbuf.c
index b4f7955..b951c7b 100644
--- a/printbuf.c
+++ b/printbuf.c
@@ -47,6 +47,14 @@ struct printbuf* printbuf_new(void)
}
+/**
+ * Extend the buffer p so it has a size of at least min_size.
+ *
+ * If the current size is large enough, nothing is changed.
+ *
+ * Note: this does not check the available space! The caller
+ * is responsible for performing those calculations.
+ */
static int printbuf_extend(struct printbuf *p, int min_size)
{
char *t;
@@ -55,11 +63,11 @@ static int printbuf_extend(struct printbuf *p, int min_size)
if (p->size >= min_size)
return 0;
- new_size = json_max(p->size * 2, p->bpos + min_size + 8);
+ new_size = json_max(p->size * 2, min_size + 8);
#ifdef PRINTBUF_DEBUG
MC_DEBUG("printbuf_memappend: realloc "
- "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
- p->bpos, size, p->size, new_size);
+ "bpos=%d min_size=%d old_size=%d new_size=%d\n",
+ p->bpos, min_size, p->size, new_size);
#endif /* PRINTBUF_DEBUG */
if(!(t = (char*)realloc(p->buf, new_size)))
return -1;
@@ -70,8 +78,8 @@ static int printbuf_extend(struct printbuf *p, int min_size)
int printbuf_memappend(struct printbuf *p, const char *buf, int size)
{
- if(p->size - p->bpos <= size) {
- if (printbuf_extend(p, size) < 0)
+ if (p->size <= p->bpos + size + 1) {
+ if (printbuf_extend(p, p->bpos + size + 1) < 0)
return -1;
}
memcpy(p->buf + p->bpos, buf, size);
@@ -87,7 +95,7 @@ int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
if (offset == -1)
offset = pb->bpos;
size_needed = offset + len;
- if(pb->size - pb->bpos <= size_needed)
+ if (pb->size < size_needed)
{
if (printbuf_extend(pb, size_needed) < 0)
return -1;