From e7ba91a6c05d884cc6745d8e49faeb1a92909679 Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Fri, 7 Apr 2023 16:56:42 -0700 Subject: [PATCH 1/3] sds: fix off by 1 bug in flb_sds_printf Signed-off-by: Wesley Pettit --- src/flb_sds.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/flb_sds.c b/src/flb_sds.c index 8fe0caecbf4..982bac30319 100644 --- a/src/flb_sds.c +++ b/src/flb_sds.c @@ -434,8 +434,8 @@ flb_sds_t flb_sds_printf(flb_sds_t *sds, const char *fmt, ...) } va_end(ap); - if (size > flb_sds_avail(s)) { - tmp = flb_sds_increase(s, size); + if (size >= flb_sds_avail(s)) { + tmp = flb_sds_increase(s, size + 1); if (!tmp) { return NULL; } From 6c9e49a627931bd1bdbd8d965a64bfd5c325e01d Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Mon, 10 Apr 2023 10:33:44 -0700 Subject: [PATCH 2/3] sds: flb_sds_printf: flb_sds_increase increases by not to Signed-off-by: Wesley Pettit --- src/flb_sds.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/flb_sds.c b/src/flb_sds.c index 982bac30319..2cb562c868f 100644 --- a/src/flb_sds.c +++ b/src/flb_sds.c @@ -417,8 +417,8 @@ flb_sds_t flb_sds_printf(flb_sds_t *sds, const char *fmt, ...) if (len < 64) len = 64; s = *sds; - if (flb_sds_avail(s)< len) { - tmp = flb_sds_increase(s, len); + if (flb_sds_avail(s) < len) { + tmp = flb_sds_increase(s, len - flb_sds_avail(s)); if (!tmp) { return NULL; } @@ -435,7 +435,7 @@ flb_sds_t flb_sds_printf(flb_sds_t *sds, const char *fmt, ...) va_end(ap); if (size >= flb_sds_avail(s)) { - tmp = flb_sds_increase(s, size + 1); + tmp = flb_sds_increase(s, size - flb_sds_avail(s) + 1); if (!tmp) { return NULL; } From f45b3027dfd0ebac20e35df16bed14020718b780 Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Mon, 10 Apr 2023 10:53:54 -0700 Subject: [PATCH 3/3] tests: internal: sds: add test for #7143 off by 1 bug Signed-off-by: Wesley Pettit --- tests/internal/sds.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/internal/sds.c b/tests/internal/sds.c index aa72291f6be..30dc166ddb1 100644 --- a/tests/internal/sds.c +++ b/tests/internal/sds.c @@ -37,6 +37,36 @@ static void test_sds_printf() flb_sds_destroy(s); } +/* https://github.com/fluent/fluent-bit/issues/7143 */ +static void test_sds_printf_7143_off_by_1() +{ + flb_sds_t tmp; + flb_sds_t test; + flb_sds_t test2; + int len; + + /* 66 char final string, not impacted by bug */ + test = flb_sds_create_size(64); + TEST_CHECK(test != NULL); + tmp = flb_sds_printf(&test, "A0123456789 %s", "this-is-54-chars-1234567890-abcdefghijklmnopqrstuvwxyz"); + TEST_CHECK(tmp != NULL); + len = flb_sds_len(test); + TEST_CHECK(len == 66); + TEST_CHECK(test[len -1] == 'z'); + flb_sds_destroy(test); + + /* 65 char final string, impacted by bug */ + test2 = flb_sds_create_size(64); + TEST_CHECK(test2 != NULL); + tmp = flb_sds_printf(&test2, "0123456789 %s", "this-is-54-chars-1234567890-abcdefghijklmnopqrstuvwxyz"); + TEST_CHECK(tmp != NULL); + len = flb_sds_len(test2); + TEST_CHECK(len == 65); + TEST_CHECK(test2[len -1] == 'z'); + flb_sds_destroy(test2); + +} + static void test_sds_cat_utf8() { flb_sds_t s; @@ -53,5 +83,6 @@ TEST_LIST = { { "sds_usage" , test_sds_usage}, { "sds_printf", test_sds_printf}, { "sds_cat_utf8", test_sds_cat_utf8}, + { "test_sds_printf_7143_off_by_1", test_sds_printf_7143_off_by_1}, { 0 } };