From dbbda02f70f4d1bb163a631bb39b0d339c4ce4a0 Mon Sep 17 00:00:00 2001
From: Klaus Wagner <nenioscio@gmail.com>
Date: Thu, 27 Feb 2014 22:12:46 +0100
Subject: [PATCH] Fix issue #338

---
 mongoose.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/mongoose.c b/mongoose.c
index a417be80c..22c4d3b19 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -2262,11 +2262,13 @@ void mg_printf_data(struct mg_connection *c, const char *fmt, ...) {
   struct connection *conn = MG_CONN_2_CONN(c);
   struct iobuf *io = &conn->ns_conn->send_iobuf;
   va_list ap;
-  int len, n;
+  int len, n, iolen;
   char *p;
 
   terminate_headers(c);
 
+  // Remember original io->len
+  iolen = io->len;
   // Write the placeholder for the chunk size
   p = io->buf + io->len;
   iobuf_append(io, "        \r\n", 10);
@@ -2276,6 +2278,9 @@ void mg_printf_data(struct mg_connection *c, const char *fmt, ...) {
   len = ns_vprintf(conn->ns_conn, fmt, ap);
   va_end(ap);
 
+  // Recalculate pointer p because of potential realloc within iobuf_append
+  p = io->buf + iolen;
+
   // Record size
   n = mg_snprintf(p, 7, "%X", len);
   p[n] = ' ';
-- 
GitLab