limit string length
diff --git a/src/core/ngx_string.c b/src/core/ngx_string.c
index 11f1cf2..acd9382 100644
--- a/src/core/ngx_string.c
+++ b/src/core/ngx_string.c
@@ -227,7 +227,9 @@
                     }
 
                 } else {
-                    buf = ngx_cpymem(buf, p, slen);
+                    len = (buf + slen < last) ? slen : (size_t) (last - buf);
+
+                    buf = ngx_cpymem(buf, p, len);
                 }
 
                 fmt++;