From fdbefc9d839c23442f773d340b2a65a03c77b427 Mon Sep 17 00:00:00 2001
From: "yoav@monfort.co.il" <yoav@monfort.co.il>
Date: Sun, 7 Feb 2016 20:24:28 +0200
Subject: [PATCH] cmsgpack: pass correct osize values to lua allocator, update
 correct buf free space in cmsgpack

---
 deps/lua/src/lua_cmsgpack.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/deps/lua/src/lua_cmsgpack.c b/deps/lua/src/lua_cmsgpack.c
index 0b82d008..7b8adb97 100644
--- a/deps/lua/src/lua_cmsgpack.c
+++ b/deps/lua/src/lua_cmsgpack.c
@@ -119,10 +119,10 @@ mp_buf *mp_buf_new(lua_State *L) {
 
 void mp_buf_append(mp_buf *buf, const unsigned char *s, size_t len) {
     if (buf->free < len) {
-        size_t newlen = buf->len+len;
+        size_t newsize = (buf->len+len)*2;
 
-        buf->b = (unsigned char*)mp_realloc(buf->L, buf->b, buf->len, newlen*2);
-        buf->free = newlen;
+        buf->b = (unsigned char*)mp_realloc(buf->L, buf->b, buf->len + buf->free, newsize);
+        buf->free = newsize - buf->len;
     }
     memcpy(buf->b+buf->len,s,len);
     buf->len += len;
@@ -130,7 +130,7 @@ void mp_buf_append(mp_buf *buf, const unsigned char *s, size_t len) {
 }
 
 void mp_buf_free(mp_buf *buf) {
-    mp_realloc(buf->L, buf->b, buf->len, 0); /* realloc to 0 = free */
+    mp_realloc(buf->L, buf->b, buf->len + buf->free, 0); /* realloc to 0 = free */
     mp_realloc(buf->L, buf, sizeof(*buf), 0);
 }