Skip to content

Commit

Permalink
demo: sds off by one bug in specific conditions
Browse files Browse the repository at this point in the history
Signed-off-by: Wesley Pettit <[email protected]>
  • Loading branch information
PettitWesley committed Apr 7, 2023
1 parent 44a205f commit 57925b8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
25 changes: 25 additions & 0 deletions plugins/out_stdout/stdout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions src/flb_sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down

0 comments on commit 57925b8

Please sign in to comment.