Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sds: fix off by 1 bug in flb_sds_printf #7148

Merged
merged 3 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/flb_sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 - flb_sds_avail(s) + 1);
if (!tmp) {
return NULL;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/internal/sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 }
};