BUG: errors: remove printf positional args for user messages context
Change the algorithm for the generation of the user messages context
prefix. Remove the dubious API relying on optional printf positional
arguments. This may be non portable, and in fact the CI glibc crashes
with the following error when some arguments are not present in the
format string :
"invalid %N$ use detected".
Now, a fixed buffer attached to the context instance is allocated once
for the program lifetime. Then call repeatedly snprintf with the
optional arguments of context if present to build the context string.
The buffer is deallocated via a per-thread free handler.
This does not need to be backported.
diff --git a/src/errors.c b/src/errors.c
index f1e1925..d2eafce 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -25,7 +25,8 @@
/* A thread local context used for stderr output via ha_alert/warning/notice/diag.
*/
-static THREAD_LOCAL struct usermsgs_ctx usermsgs_ctx = { };
+#define USERMSGS_CTX_BUFSIZE PATH_MAX
+static THREAD_LOCAL struct usermsgs_ctx usermsgs_ctx = { .str = BUF_NULL, };
/* Put msg in usermsgs_buf.
*
@@ -118,35 +119,49 @@
usermsgs_ctx.obj = NULL;
}
-static void generate_usermsgs_ctx_str(char **str)
+static void generate_usermsgs_ctx_str(void)
{
- const struct usermsgs_ctx *ctx = &usermsgs_ctx;
- const char prefix_fmt[] = "%1$s : ";
- const char file_line_fmt[] = "[%2$s:%3$d] : ";
- const char obj_line_fmt[] = "%4$s/%5$s";
+ struct usermsgs_ctx *ctx = &usermsgs_ctx;
+ void *area;
+ int ret;
- /* fmt must be big enough to contains the full format string before
- * memprintf */
- char fmt[56];
+ if (unlikely(b_is_null(&ctx->str))) {
+ area = calloc(USERMSGS_CTX_BUFSIZE, sizeof(char));
+ if (area)
+ ctx->str = b_make(area, USERMSGS_CTX_BUFSIZE, 0, 0);
+ }
+
+ if (likely(!b_is_null(&ctx->str))) {
+ b_reset(&ctx->str);
+
+ if (ctx->prefix) {
+ ret = snprintf(b_tail(&ctx->str), b_room(&ctx->str),
+ "%s : ", ctx->prefix);
+ b_add(&ctx->str, MIN(ret, b_room(&ctx->str)));
+ }
- switch (obj_type(ctx->obj)) {
- case OBJ_TYPE_SERVER:
- sprintf(fmt, "%s%s'server %s' : ",
- ctx->prefix ? prefix_fmt : "",
- ctx->file ? file_line_fmt : "",
- obj_line_fmt);
- memprintf(str, fmt, ctx->prefix, ctx->file, ctx->line,
- __objt_server(ctx->obj)->proxy->id,
- __objt_server(ctx->obj)->id);
- break;
+ if (ctx->file) {
+ ret = snprintf(b_tail(&ctx->str), b_room(&ctx->str),
+ "[%s:%d] : ", ctx->file, ctx->line);
+ b_add(&ctx->str, MIN(ret, b_room(&ctx->str)));
+ }
+
+ switch (obj_type(ctx->obj)) {
+ case OBJ_TYPE_SERVER:
+ ret = snprintf(b_tail(&ctx->str), b_room(&ctx->str),
+ "'server %s/%s' : ",
+ __objt_server(ctx->obj)->proxy->id,
+ __objt_server(ctx->obj)->id);
+ b_add(&ctx->str, MIN(ret, b_room(&ctx->str)));
+ break;
+
+ case OBJ_TYPE_NONE:
+ default:
+ break;
+ }
- case OBJ_TYPE_NONE:
- default:
- sprintf(fmt, "%s%s",
- ctx->prefix ? prefix_fmt : "",
- ctx->file ? file_line_fmt : "");
- memprintf(str, fmt, ctx->prefix, ctx->file, ctx->line);
- break;
+ if (!b_data(&ctx->str))
+ snprintf(b_tail(&ctx->str), b_room(&ctx->str), "%s", "");
}
}
@@ -175,11 +190,12 @@
msg_ist.len--;
if (use_usermsgs_ctx) {
- generate_usermsgs_ctx_str(&parsing_str);
+ generate_usermsgs_ctx_str();
+ parsing_str = b_head(&usermsgs_ctx.str);
reset_usermsgs_ctx();
}
else {
- memprintf(&parsing_str, "%s", "");
+ parsing_str = "";
}
if (global.mode & MODE_STARTING) {
@@ -204,7 +220,6 @@
fflush(stderr);
free(head);
- free(parsing_str);
free(msg);
}
@@ -357,6 +372,7 @@
{
ring_free(_HA_ATOMIC_XCHG(&startup_logs, NULL));
ha_free(&usermsgs_buf.area);
+ ha_free(&usermsgs_ctx.str.area);
}
REGISTER_PER_THREAD_FREE(deinit_errors_buffers);