]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stats: make show info json future-proof
authorAurelien DARRAGON <adarragon@haproxy.com>
Thu, 15 Dec 2022 13:24:30 +0000 (14:24 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 15 Dec 2022 15:53:49 +0000 (16:53 +0100)
This is a follow up of "BUG/MINOR: stats: fix show stat json buffer limitation"

However this time this is purely preemptive as we did not reach the buffer
limitation yet. But now is the proper time so that this won't be an issue
in the upcoming versions.

No backport needed.

src/stats.c

index ff49d15b8fd8f3cfa76882bbdfa9d198d4dd62a0..dad626172fbc100fe0bb953e6a730616d6d6f602 100644 (file)
@@ -661,14 +661,15 @@ static int stats_dump_json_info_fields(struct buffer *out,
                                        const struct field *info,
                                        struct show_stat_ctx *ctx)
 {
-       int field;
-       int started = 0;
+       int started = (ctx->field) ? 1 : 0;
+       int ready_data = 0;
 
-       if (!chunk_strcat(out, "["))
+       if (!started && !chunk_strcat(out, "["))
                return 0;
 
-       for (field = 0; field < INF_TOTAL_FIELDS; field++) {
+       for (; ctx->field < INF_TOTAL_FIELDS; ctx->field++) {
                int old_len;
+               int field = ctx->field;
 
                if (!field_format(info, field))
                        continue;
@@ -694,16 +695,24 @@ static int stats_dump_json_info_fields(struct buffer *out,
 
                if (!chunk_strcat(out, "}"))
                        goto err;
+               ready_data = out->data;
        }
 
        if (!chunk_strcat(out, "]\n"))
                goto err;
+       ctx->field = 0; /* we're done */
        return 1;
 
 err:
-       chunk_reset(out);
-       chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
-       return 0;
+       if (!ready_data) {
+               /* not enough buffer space for a single entry.. */
+               chunk_reset(out);
+               chunk_appendf(out, "{\"errorStr\":\"output buffer too short\"}\n");
+               return 0; /* hard error */
+       }
+       /* push ready data and wait for a new buffer to complete the dump */
+       out->data = ready_data;
+       return 1;
 }
 
 static void stats_print_proxy_field_json(struct buffer *out,