6464#include " HttpTransact.h"
6565#include " Main.h"
6666#include " ts/RawHashTable.h"
67+ #include " ts/ink_sprintf.h"
6768
6869#define HTTP_BODY_TEMPLATE_MAGIC 0xB0DFAC00
6970#define HTTP_BODY_SET_MAGIC 0xB0DFAC55
@@ -156,7 +157,8 @@ class HttpBodyFactory
156157 // /////////////////////
157158 char *fabricate_with_old_api (const char *type, HttpTransact::State *context, int64_t max_buffer_length,
158159 int64_t *resulting_buffer_length, char *content_language_out_buf, size_t content_language_buf_size,
159- char *content_type_out_buf, size_t content_type_buf_size, const char *format, va_list ap);
160+ char *content_type_out_buf, size_t content_type_buf_size, int format_size, const char *format,
161+ va_list ap);
160162
161163 char *
162164 fabricate_with_old_api_build_va (const char *type, HttpTransact::State *context, int64_t max_buffer_length,
@@ -166,12 +168,20 @@ class HttpBodyFactory
166168 {
167169 char *msg;
168170 va_list ap;
169-
170- va_start (ap, format);
171- msg = fabricate_with_old_api (type, context, max_buffer_length, resulting_buffer_length, content_language_out_buf,
172- content_language_buf_size, content_type_out_buf, content_type_buf_size, format, ap);
173- va_end (ap);
174- return msg;
171+ if (format) {
172+ // The length from ink_bvsprintf includes the trailing NUL, so adjust the final
173+ // length accordingly.
174+ int l = ink_bvsprintf (nullptr , format, ap);
175+ if (l <= max_buffer_length) {
176+ msg = (char *)ats_malloc (l);
177+ *resulting_buffer_length = ink_bvsprintf (msg, format, ap) - 1 ;
178+ }
179+ }
180+ char *buf = fabricate_with_old_api (type, context, max_buffer_length, resulting_buffer_length, content_language_out_buf,
181+ content_language_buf_size, content_type_out_buf, content_type_buf_size,
182+ *resulting_buffer_length, msg, ap);
183+ ats_free (msg);
184+ return buf;
175185 }
176186
177187 void dump_template_tables (FILE *fp = stderr);
0 commit comments