Skip to content

Commit 0807f32

Browse files
committed
TS-2888: remove varargs from fabricate
1 parent 1499975 commit 0807f32

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

proxy/http/HttpBodyFactory.cc

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ char *
6363
HttpBodyFactory::fabricate_with_old_api(const char *type, HttpTransact::State *context, int64_t max_buffer_length,
6464
int64_t *resulting_buffer_length, char *content_language_out_buf,
6565
size_t content_language_buf_size, char *content_type_out_buf, size_t content_type_buf_size,
66-
const char *format, va_list ap)
66+
int format_size, const char *format, va_list ap)
6767
{
6868
char *buffer = nullptr;
6969
const char *lang_ptr = nullptr;
@@ -124,16 +124,8 @@ HttpBodyFactory::fabricate_with_old_api(const char *type, HttpTransact::State *c
124124
///////////////////////////////////////////
125125
// check if we don't need to format body //
126126
///////////////////////////////////////////
127-
if (format) {
128-
// The length from ink_bvsprintf includes the trailing NUL, so adjust the final
129-
// length accordingly.
130-
int l = ink_bvsprintf(nullptr, format, ap);
131-
if (l <= max_buffer_length) {
132-
buffer = (char *)ats_malloc(l);
133-
*resulting_buffer_length = ink_bvsprintf(buffer, format, ap) - 1;
134-
plain_flag = true;
135-
}
136-
}
127+
128+
buffer = (format == nullptr) ? nullptr : ats_strndup(format, format_size);
137129
/////////////////////////////////////////////////////////
138130
// try to fabricate the desired type of error response //
139131
/////////////////////////////////////////////////////////

proxy/http/HttpBodyFactory.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
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);

proxy/http/HttpTransact.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8199,8 +8199,8 @@ HttpTransact::build_error_response(State *s, HTTPStatus status_code, const char
81998199
char *new_msg;
82008200

82018201
new_msg = body_factory->fabricate_with_old_api(error_body_type, s, 8192, &len, body_language, sizeof(body_language), body_type,
8202-
sizeof(body_type), s->internal_msg_buffer_size ? s->internal_msg_buffer : nullptr,
8203-
nullptr);
8202+
sizeof(body_type), s->internal_msg_buffer_size,
8203+
s->internal_msg_buffer_size ? s->internal_msg_buffer : nullptr, nullptr);
82048204

82058205
// After the body factory is called, a new "body" is allocated, and we must replace it. It is
82068206
// unfortunate that there's no way to avoid this fabrication even when there is no substitutions...

0 commit comments

Comments
 (0)