From 57925b8f0ac90d9454cd391cf873410b7d847a08 Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Fri, 7 Apr 2023 16:54:30 -0700 Subject: [PATCH] demo: sds off by one bug in specific conditions Signed-off-by: Wesley Pettit --- plugins/out_stdout/stdout.c | 25 +++++++++++++++++++++++++ src/flb_sds.c | 6 ++++++ 2 files changed, 31 insertions(+) diff --git a/plugins/out_stdout/stdout.c b/plugins/out_stdout/stdout.c index aa797426058..7338bc26a0a 100644 --- a/plugins/out_stdout/stdout.c +++ b/plugins/out_stdout/stdout.c @@ -174,6 +174,31 @@ static void cb_stdout_flush(struct flb_event_chunk *event_chunk, ctx = (struct flb_stdout *) out_context; cnt = 0; + flb_sds_t tmp; + flb_sds_t test = flb_sds_create_size(64); + if (!test) { + flb_errno(); + FLB_OUTPUT_RETURN(FLB_ERROR); + } + + flb_info("Example below will work as expected, note that all letters up to z are printed:"); + + tmp = flb_sds_printf(&test, "A0123456789 %s", "this-is-54-chars-1234567890-abcdefghijklmnopqrstuvwxyz"); + flb_info("TEST: %s", tmp); + + flb_sds_t test2 = flb_sds_create_size(64); + if (!test2) { + flb_errno(); + FLB_OUTPUT_RETURN(FLB_ERROR); + } + + flb_info("Example below shows bug, note that all letters up to z are not printed: "); + tmp = flb_sds_printf(&test2, "123456789 %s", "this-is-54-chars-1234567890-abcdefghijklmnopqrstuvwxyz"); + flb_info("TEST: %s", tmp); + + + + #ifdef FLB_HAVE_METRICS /* Check if the event type is metrics, handle the payload differently */ if (event_chunk->type == FLB_EVENT_TYPE_METRICS) { diff --git a/src/flb_sds.c b/src/flb_sds.c index 8fe0caecbf4..159d76843a7 100644 --- a/src/flb_sds.c +++ b/src/flb_sds.c @@ -416,6 +416,8 @@ flb_sds_t flb_sds_printf(flb_sds_t *sds, const char *fmt, ...) if (len < 64) len = 64; + flb_info("[sds] len=%d", len); + s = *sds; if (flb_sds_avail(s)< len) { tmp = flb_sds_increase(s, len); @@ -427,6 +429,7 @@ flb_sds_t flb_sds_printf(flb_sds_t *sds, const char *fmt, ...) va_start(ap, fmt); size = vsnprintf((char *) (s + flb_sds_len(s)), flb_sds_avail(s), fmt, ap); + flb_info("[sds] size1=%d", size); if (size < 0) { flb_warn("[%s] buggy vsnprintf return %d", __FUNCTION__, size); va_end(ap); @@ -450,11 +453,14 @@ flb_sds_t flb_sds_printf(flb_sds_t *sds, const char *fmt, ...) } va_end(ap); } + flb_info("[sds] size2=%d", size); head = FLB_SDS_HEADER(s); head->len += size; s[head->len] = '\0'; + flb_info("[sds] head->len=%d", head->len); + return s; }