From 30f5a3eb50f4dc8d551ca6dc54d225def603435b Mon Sep 17 00:00:00 2001 From: Wesley Pettit Date: Mon, 10 Apr 2023 10:53:54 -0700 Subject: [PATCH] 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 } };